1

Good day everyone.

I have an issue with the compiling process of my code. I have tested my code in the IDE that i am currently using (Codeblocks) with a testfile.txt in the same directory containing 11 words. I am 100% positive that my text file is not the issue as I have had the program run perfectly without issues. The issue that arises is that when i try to compile my code using Cygwin64 (i am on a 64bit machine) it throws this error which i have summarized below.

Code

#include <iostream>
#include <string>
#include <fstream> // need for opening file

using namespace std;

int main (){

    string filename = "testfile.txt"; // setting our pre-default test file

    ifstream file(filename); // open file
    string words; // initialize variable
    int wordcount = 0 ; // word count set at 0

    /* The operator >> skips only leading whitespace.
    /It stops extracting when it reaches the end of all words in the file
    // for every iteration add one to word count. display results.
    */
    while(file >> words)
    {
        ++wordcount;
    }

    cout<< "The number of words inside the file is : "<<wordcount<<endl;

return 0;
}

The error that i receive is as follows:

$ g++ -o oTMA1Question1 TMA1Question1.cpp
TMA1Question1.cpp: In function 'int main()':
TMA1Question1.cpp:92:13: error: no match for 'operator>>' (operand types are 'std::ifstream() {aka std::basic_ifstream<char>()}' and 'std::string {aka std::basic_string<char>}')
  while (file >> words)
              ^

And then its lists candidate from the cygwin library.

After reading this error i have re-ran/rebuild this code in a different folder with a different text file using Codeblocks and the code works without issue. However when i make a new file and do the same process (i.e compiling using cygwin) , i get the exact error.

I don't quite understand why the operand is having an issue with a match for ">>" -- if i am correct this is the general sign for "input" so in the while (file >> words) should be "while inside file traversing through words..." . Any help to make me understand the problem would be much appreciated.

0 Answers0