1

I am writing a simple class and get some error. The header file is shown below:

//
//  temp.h
//

#ifndef _TEMP_h
#define _TEMP_h

#include <string>
using namespace std;

class GameEntry {
public:
  GameEntry(const string &n="", int s=0);
  string getName();
  int getScore();
private:
  string name;
  int score;
};

#endif

And the method file is shown below:

// temp.cpp

#include "temp.h"
#include <string>
using namespace std;

GameEntry::GameEntry(const string &n, int s):name(n),score(s) {}

string GameEntry::getName() { return name; }

int GameEntry::getScore() { return score; }

The main file is shown below:

#include <iostream>
#include <string>
#include "temp.h"
using namespace std;

int main() {
  string str1 = "Kenny";
  int k = 10;
  GameEntry G1(str1,k);

  return 0;
}

I got error like this:

Undefined symbols for architecture x86_64:
  "GameEntry::GameEntry(std::__1::basic_string<char, std::__1::char_traits<char>,
                        std::__1::allocator<char> > const&, int)", referenced from:
      _main in main1-272965.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Can anyone tell me what's wrong? Many thanks.

Kenny
  • 355
  • 1
  • 5
  • 14
  • How did you invoke the compiler and linker? Seems you've forgot to link with `temp.o`. – 5gon12eder Jan 09 '15 at 22:25
  • How are you compiling? What is the command line? – joelmeans Jan 09 '15 at 22:26
  • I just simply try "c++ main.cpp", and it usually works for me. – Kenny Jan 09 '15 at 22:29
  • You will need to compile `temp.cpp` to an object file (`temp.o`) and link it with main.cpp. Try `g++ -c temp.cpp` and `g++ main.cpp temp.o`. This is in addition to the corrections in the answers below. – joelmeans Jan 09 '15 at 22:37
  • Yes this solved the problem. However, I don't quite understand. I think for similar implementation, even larger classes, I usually just include the header file in main, and only use "c++ main.cpp". Can you provide more reason of doing this? – Kenny Jan 09 '15 at 22:42
  • I created an answer below. If it helps, please accept it. Thanks. – joelmeans Jan 09 '15 at 22:47

3 Answers3

2

You cannot put the default parameters in the definition:

GameEntry::GameEntry(const string &n, int s):name(n),score(s) {}

Edit: Actually you can put it in the definition, but you cannot put it in both the definition and declaration. More information can be found in this question: Where to put default parameter value in C++?

Community
  • 1
  • 1
clcto
  • 9,530
  • 20
  • 42
  • You mean in the class file? I took out the default values, but the error is still there. – Kenny Jan 09 '15 at 22:28
  • At the definition, in the `.cpp` file. – clcto Jan 09 '15 at 22:29
  • You can just modify the header file and you are fine. @Kenny – gsamaras Jan 09 '15 at 22:30
  • @G.Samaras see here: http://stackoverflow.com/questions/4989483/where-to-put-default-parameter-value-in-c – clcto Jan 09 '15 at 22:30
  • Actually in the book, there is default value in the header and no default value in the class file. I am not clear about the reason. – Kenny Jan 09 '15 at 22:31
  • Interesting @clcto, thanks. I did what I am saying in my answer so far, guess I have to think twice. +1 for providing the link. Oh damn, the daily limit of votes is reached, sorry! – gsamaras Jan 09 '15 at 22:33
  • I already took the default value out from cpp and it is still the same error. – Kenny Jan 09 '15 at 22:34
1

You can't have defaults values both in the .h and .cpp files.

Change the prototype in the header file to this:

GameEntry(const string &n, int s);

and you good to go.

In main.cpp, you missed a semicolon here: int k = 10


An interesting link: Where to put default parameter value in C++?

Long story short, it's up to you.

If it is in the header file, it helps documentation, if it is in the source the file it actually helps the reader who reads the code and doesn't just use it.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
1

In addition to correcting the issues with default parameters, as pointed out by clcto and G. Samaras, you will need to compile temp.cpp to an object file (temp.o) and link it with main.cpp. Try this:

g++ -c temp.cpp

g++ main.cpp temp.o.

The symbols that are missing are found in the object file, which you aren't creating if you don't explicitly compile temp.cpp. I think you are probably misremembering what has worked in the past.

joelmeans
  • 122
  • 1
  • 1
  • 8
  • Thanks for all the suggestions. As for the last sentence, it is not likely although I am not expert on compiler. Would mind testing this little code. I remember it work by "c++ main.cpp". (download "sudoku.zip" at "http://www.rsmas.miami.edu/personal/wenliang.zhao/Software.htm" – Kenny Jan 09 '15 at 22:55
  • I would definitely upvote you sir if I hadn't reach the daily limit of votes, nice! – gsamaras Jan 09 '15 at 23:05