-10

Sometimes I get incredibly long errors in my code that I don't understand so I just rework my code to avoid whatever was causing the error. I had another one today that I simply can't avoid.

My code:

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <vector>

using namespace std;

void readFile(string);

class info {
    public:

    int rows;
    int cols;
    vector < string > data;
};

int main(int argc, char **argv){

    string filename1;
    filename = argv[1];
    readFile(filename);

    return 0;
}

//should read onle line at a time from a file and print it
void readFile(string filename1){
    fstream datafile;
    datafile.open(filename1);

    while (!datafile.eof()){
            string line;
            getline(datafile,line);
            cout<<line<<endl;
    }

    datafile.close();
}

The error stems from trying to get the name of the file from argv[1]. It was working fine when I just gave it the file name.

The error:

project2.cpp: In function ‘int main(int, char**)’:
project2.cpp:22:2: error: ‘filename’ was not declared in this scope
  filename = argv[1];
  ^
project2.cpp: In function ‘void readFile(std::string)’:
project2.cpp:32:25: error: no matching function for call to     ‘std::basic_fstream<char>::open(std::string&)’
  datafile.open(filename1);
                     ^
project2.cpp:32:25: note: candidate is:
In file included from project2.cpp:2:0:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/fstream:889:7: note: void     std::basic_fstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode)     [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode =     std::_Ios_Openmode]
       open(const char* __s,
       ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/fstream:889:7: note:   no     known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’     to ‘const char*’

I am using Cygwin. I used it last semester as well when I was writing code in C, and my professor had us check certain installation options at the time. Could these installation options be the root of the problem? Or are errors like this common in C++? Thanks.

  • 4
    Did you bother reading your code before posting? Your first error is just a typo... – Luke Joshua Park Feb 14 '16 at 01:44
  • Looking at your question history it appears, that software development isn't for you. Harsh as it may sound, you'll live a happier life elsewhere. – IInspectable Feb 14 '16 at 01:51
  • 2
    @IInspectable Definitely harsh. Might be better to recommend a good C++ book... – Luke Joshua Park Feb 14 '16 at 02:00
  • @LukePark: [Not everyone can learn how to code](http://blog.codinghorror.com/separating-programming-sheep-from-non-programming-goats/), and the OP appears to be one of those, that can't. I'd recommend a [good C++ book](http://stackoverflow.com/q/388242/1889329) to others. In this case, though, it's not going to lead anywhere, but prolonged agony. On either side. – IInspectable Feb 14 '16 at 02:19
  • Oh come on. You've decided that I can't learn how to code based on 3 questions? I have only used this site when I have looked everywhere and cannot find an answer to what I want to know. I have always tried to make sure my question is well written and coherent so as to take as little of the users' time as possible. My first two questions I asked when I had hardly learned any C. That first error being a typo was me trying to fix the error last minute by changing a variable name, and I've fixed that now but I still have the rest of the error, but I guess I shouldn't say that because the – SchmitsNGiggles Feb 14 '16 at 02:32
  • 5
    I do not think these error messages are "long and incoherent" - Please inform use as to why you think this is so. It has nothing to do with the set up of Cygwin. – Ed Heal Feb 14 '16 at 02:55

2 Answers2

1

Just read the error:

project2.cpp: In function ‘int main(int, char**)’: project2.cpp:22:2: error: ‘filename’ was not declared in this scope filename = argv[1]; ^

Here it says that filename is not declared. i.e. You have to declare it or something wrong with the declaration

Looking at the code you have

string filename1;

One assumes you meant

string filename;

Fix this error - then try again

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • Thank you for at least answering part of my question. – SchmitsNGiggles Feb 14 '16 at 02:44
  • I found that the root of my problem was that I had to put ".c_str()" after "filename1" in the open function, which happened simply because I've never opened a file using this method in C++. – SchmitsNGiggles Feb 14 '16 at 02:54
  • 1
    Please in future read the error message "no matching function for call to ..." then read the manual page for the method that you are calling – Ed Heal Feb 14 '16 at 02:59
0

The first error:change filename1 to filename The second error: you should set a open()functions in the class info.then you can use it