2

Every time I try to compile this program, it gets the following error:

    Undefined symbols for architecture x86_64:
  "Character::setTime(int)", referenced from:
      awesome(Character)    in ccmsAc4F.o
  "Character::getMoney()", referenced from:
      _main in ccmsAc4F.o
  "Character::setMoney(double)", referenced from:
      awesome(Character)    in ccmsAc4F.o
  "Character::Character()", referenced from:
      _main in ccmsAc4F.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

Character compiles correctly and has no errors. The problem seems to be trying to pass the object as a reference. The following is my program:

#include <cstdlib>
#include "Character.h"
#include <iostream>

using namespace std;

void awesome(Character& character);

int main() {
    Character userCharacter;
    awesome(userCharacter);
    cout << userCharacter.getMoney();
    return 0;
}

void awesome(Character& character)
{
    character.setTime(0);
    character.setMoney(0.00);
}

Does anyone have any insight on what I'm doing wrong? Ultimately in a larger program I want to use the reference to edit an already instantiated object. I'm assuming referencing is the right way to do it. Any help would be greatly appreciated.

2 Answers2

1

They are linker errors. You forget to implement methods of Character or link Character.o to your project.

Character::setTime(int) { /* body ?! */ }
Character::getMoney() { /* body ?! */ }
Character::setMoney(double) { /* body ?! */ }
Character::Character() { /* body ?! */ }
masoud
  • 55,379
  • 16
  • 141
  • 208
  • The methods are fully implemented. I'm not sure what you mean by linking Character.o to the project. – user2130768 Mar 04 '13 at 07:15
  • 2
    Did you add character.cpp to your project ot makefile? – masoud Mar 04 '13 at 07:17
  • M M, I appreciate the help. I think a better means of explanation would be how you personally would go through the process of compiling and linking this program correctly. I'm using Xcode for C++ development, and we haven't gone over anything like this in my Software Construction course. – user2130768 Mar 04 '13 at 07:43
0

You need to compile Character.cpp that will result Character.o which will finally link with your project that makes the class Character known to the linker.Then this linking errors will be gone.

Subhajit
  • 320
  • 1
  • 6