Hello,

I have wrote a OO program and I have been able to compile ok when using Dev cpp however, I have moved to mac and now compiling through the terminal.

For classes I use a main.cpp (the main script), Numbers.h (The header file for the class) and Numbers.cpp (all the methods)

Numbers.h:

class Numbers
{
	public:
		Numbers();
		
		Numbers(int theNumber1, int theNumber2);
		void setNumber1 (int theNumber1);
		void setNumber2 (int theNumber2);
		
		int getNumber1();
		int getNumber2();
		
	protected:
	
		int number1;
		int number2;
};

Numbers.cpp

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

Numbers::Numbers(){}

Numbers::Numbers(int theNumber1, int theNumber2)
{
	number1 = theNumber1;
	number2 = theNumber2;
}

void setNumber1 (int theNumber1)
{

}
void setNumber2 (int theNumber2)
{

}
		
int getNumber1()
{

}
int getNumber2()
{

}

Main.cpp:

#include <iostream>

#include "Numbers.h"

int main()
{
	Numbers number;
	
	return 0;
}

And I compile using g++ -o main main.cpp (for example)

And it gives me an error like:

Undefined symbols for architecture x86_64:
  "Numbers::Numbers()", referenced from:
      _main in cczo4aBZ.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

Any ideas? Really annoying me :(

In Numbers.cpp you failed to provide the class name in front of the function names on lines 12, 16, 21 and 25. See line 6 for the correct way to code those functions.

Heyy,

Thanks for the reply.. I've sorted the problem out now.. It was because I wasn't linking the files :)

Oh, so that means the code you posted is not the same as the code you are compiling. Next time please post current and correct code, not something you just contrived so that others don't try to solve non-existent problems.

Edited 5 Years Ago by Ancient Dragon: n/a

Sorry, I totally forgot about the class name infront of the methods (It's been a while since I coded C++)

This article has been dead for over six months. Start a new discussion instead.