Passing a string to a class constructor

I'm attempting to create a class to open a file, and read from it, by passing the filename to the constructor. I have searched the web and have tried to replicate the pertinent examples. I have managed to reduce the compiler (g++) errors to the following:

/tmp/ccBDeLHM.o: In function `main':
main.cpp:(.text+0x6a): undefined reference to `myFiles::myFiles(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status

My code is as follows:

myFiles.h
----------------------------
#ifndef MYFILES_H
#define MYFILES_H
#include <string>

class myFiles
{
public:
myFiles();
myFiles(std::string s);
};
#endif
----------------------------
myFiles.cpp
----------------------------
#include <istream>
#include <string>
#include "myfiles.h"
using namespace std;

myFiles::myFiles(); //default constructor
{
}
myFiles::myFiles(std::string fileName)
{
std::string line;
ifstream passFile;
passFile.open(fileName);

if(passFile)
{
//do some stuff
)
file.close();
}
------------------------------
main.cpp
------------------------------
#include <string>
#include "myfiles.h"
using namespace std;

int main()
{
string fileName="test.txt";
myFiles files(fileName);

return 0;
}
-------------------------------------

What am I missing?

myFiles.cpp isn't being linked.

If you are using an IDE, make sure myFiles.cpp is added as part of your project. When you do a rebuild, you should see it compiling. If you don't see it compiling then you know it's not part of your project.


If you aren't using an IDE -- get one. It will make your life 100x easier. To do it without an IDE you have to give the commandline some weird option thing. I don't really know how to do it by hand in g++.
Also, in the constructor myFiles::myFiles(std::string fileName)

The line passFile.open(fileName); will not work because for historical reasons the open method takes a C style string and not a std::string. Change it to:

passFile.open( fileName.c_str() );
Thanks for the assistance. I finally have it compiling and functioning, with the code listed below:

MAIN.CPP
..........................................................................................................................
#include <iostream>
#include <string>

#include "myFiles.h"

int main(int argc, char *argv[])
{
char fileName[] = "test.txt";

cout<< "In main the fileName is "<<fileName<<endl;

myFiles files;
files.getInfo(fileName);

return 0;
}
................................................................................................................

MYFILES.CPP
................................................................................................................
#ifndef MYFILES_H
#define MYFILES_H

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class myFiles
{
public:
void getInfo(char fileName[]);
};

#endif // MYFILES_H

void myFiles::getInfo(char fileName[])
{
cout<<"In getInfo the fileName is " <<fileName<<endl;

ifstream passFile;
passFile.open(fileName, ios::in);

if(passFile.is_open())
{
cout<<"File Is Open" <<endl;
//Do some stuff
passFile.close();
}
else
{
cout<<"File Is NOT Open"<<endl;
}
}

Topic archived. No new replies allowed.