-1

Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?

Someone wants to save me some time on how classes and constructors work in c++ ? This is what I got - it do not work. I want to the class to have an constructor that takes a filename and reads a file with that name from the filesystem.

this is the header and implementation

#ifndef __narcissism__Histogram__
#define __narcissism__Histogram__

#include <iostream>
#include <sstream>  // for ostringstream
#include <iomanip>  // for setw, setfill
#include <ios>      // for hex stream manipulator
using namespace std;
#include "random.h" // for randomInteger
#include "strlib.h" // for integerToString
#include "error.h"  // for error



class Histogram {
public:

/** Constructor:
  * 
  *  */
Histogram(string filename)
{
    readfile(filename);

}


private:

int readfile(string filename);

};




#endif /* defined(__narcissism__Histogram__) */

*.cpp

 #include "Histogram.h"



 int readfile(string filename)
 {
 return 0;
 }

Error msg:

Undefined symbols for architecture i386:
"Histogram::readfile(std::string)", referenced from:
  Histogram::Histogram(std::string) in narcissism.o
ld: symbol(s) not found for architecture i386
Community
  • 1
  • 1
Tom Lilletveit
  • 1,872
  • 3
  • 31
  • 57

2 Answers2

2

You must add Histogram:: in the definition of the member function:

 int Histogram::readfile(string filename)
 {
 return 0;
 }

Otherwise it would define a new global function with the same name, leaving the member function undefined.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
2

Your error is that readfile is a member of Histogram so in the .cpp file it should be:

int Histogram::readfile( string filename )
{
     // implement
}

The function you wrote though would actually, at this point, be a valid function. (It would fail in compilation if it tried to access of any Histogram's members, which presumably a proper implementation of readfile would: the purpose is surely to set these members from data it reads from the file).

You instead got a link error because there was no implementation defined for the function called readfile that is a member of the class Histogram.

CashCow
  • 30,981
  • 5
  • 61
  • 92