4

I created a simple class 'Hello' in C++ using header(.h) and definition(.cpp) files. This is the header file content:

#ifndef HELLO_H
#define HELLO_H

#include <string>

namespace test
{
    class Hello
    {
    private:
        std::string name;

    public:
        Hello();
        void say_hello();
    };
}

#endif

And the definition file content is just as you expected:

#include "Hello.h"
#include <iostream.h>

using namespace test;

Hello::Hello()
{
    this->name = "Yoppy Yunhasnawa";
}

void Hello::say_hello()
{
    string message = "Hello, " + this->name + ".. Have nice day!";

    cout << message << "\n";
}

I included this class to a main.cpp file and use it like this:

#include "Hello.h"

using namespace test;

int main(int argc, char *argv[])
{   
    Hello* hello = new Hello;

    hello->say_hello();
}

When I compiled the main.cpp file with g++ like this,

g++ main.cpp

I got following annoying error:

Undefined symbols for architecture x86_64:
  "test::Hello::say_hello()", referenced from:
      _main in ccsaoOZa.o
  "test::Hello::Hello()", referenced from:
      _main in ccsaoOZa.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

However, that error does not appear when I don't call both constructor and say_hello method:

int main(int argc, char *argv[])
{   
    Hello* hello;// = new Hello;

    //hello->say_hello();
}

I use macport GCC 4.7 and I am very sure that my method is there but why this symbol(s) not found error keep appearing? Please show me my mistake. Thank you.

yunhasnawa
  • 815
  • 1
  • 14
  • 30
  • Please don't needlessly use pointers and `new`. `Hello hello; hello.say_hello();` – chris Jun 15 '13 at 15:07
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – chris Jun 15 '13 at 15:09

2 Answers2

10

When you invoke g++ main.cpp, compiler performs both compiling AND linking. But the code cannot be linked without Hello.cpp file. So, you have two options: either compile and link separately:

g++ -c main.cpp
g++ -c hello.cpp
gcc main.o hello.o

or compile and link everything at the same time:

g++ main.cpp hello.cpp
nullptr
  • 11,008
  • 1
  • 23
  • 18
  • Thank you for the answer Sir. Sorry but, I need to include many classes in main.cpp and compile dynamically from other program by using console, is there any method that allow me to unite all needed header (or precompile it?) so I don't have to compile all *.cpp? Like `g++ allheader.a main.cpp` instead of `g++ a.cpp b.cpp c.cpp d.cpp main.cpp` for example. – yunhasnawa Jun 15 '13 at 17:32
  • 2
    Yes, you can create a static library: first compile all the .cpp modules except main with `-c` option: `g++ -c a.cpp`, `g++ -c b.cpp` etc. (so you have `.o` files) and then create a library with `ar rcs allheader.a a.o b.o c.o d.o`. Then you'll be able to perform `g++ main.cpp allheader.a`. – nullptr Jun 15 '13 at 17:45
0

This Problem of unidentified class in c++ program can be easily solved by two methods:

  1. Declare the classes at the top .

  2. Declare the prototype of the functions in the class and define them at the end of the program.

     #include<iostream>
     using namespace std;
    
     class A;
     class B;
    
     class A
     {
         int a;
     public:
     void Print()
     {
         cout << a;
     }
     };
     class B
     {
         int b;
     public:
     void Print()
     {
         cout << b;
     }
     };
     void main()
     {
         A obja;
         B objb;
     }
    

Or you can do that:

#include<iostream>
using namespace std;

class A;
class B;

class A
{
    int a;
public:
    void Print();
};
class B
{
    int b;
public:
    void Print();
};


void A::Print()
{
    cout << a;
}
void B::Print()
{
    cout << b;
}
MAFHH
  • 1
  • 1