3

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

I know this problem gets answered all the time, but I haven't been able to find a solution for my specific example. Here's the full error:

g++ main.cpp
Undefined symbols for architecture x86_64:
  "Board::display()", referenced from:
  _main in cc7hPZpy.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

I'm just trying to pull this display function from my Board class. Here's main:

 #include "Board.h"
 #include <iostream>
 #include <string>

 using namespace std;

 int main()
 {
    cout << "Some Asian Game" << endl;
    Board base;
    base.display();
            //this is creating the error
            //commenting it out compiles, but obviously does not do what i want. 


    return 0;
 }

and Board.h:

#ifndef Board_H
#define Board_H

#include "Row.h"
#include <vector>
using namespace std;

class Board
{
public:
   vector<Row> rows;

   Board()
   {
        vector<Row> (15);
   }

   void play(int row, int col, char clr);
   bool checkWin(int row, int col, char clr);
   char getCellColor(int row, int col);
   void display();

   void empty();
};

#endif

and the particular function in question from board.cpp

void Board::display()
{
    for(int i=0;i<16;i++)
    {
         for(int i2=0;i2<16;i2++)
         {
           cout << rows[i].cells[i2].getState();
         }
    }
}

I've referred to this site often for answers, but have never used it personally, so bear with me on responding. I'm pretty positive this is something simple that I'm just overlooking though.

Community
  • 1
  • 1
  • Does http://stackoverflow.com/a/12574400/673730 help? I.e. are you sure you're compiling `board.cpp` and linking against the object file? (just noticed the command line you use, and no, you're not) – Luchian Grigore Oct 10 '12 at 22:04

1 Answers1

2

It should be

g++ main.cpp board.cpp

You're not compiling board.cpp so the symbol is not exported.

Also:

Board()
{
    vector<Row> (15);
}

is wrong. It just creates a temporary, you probably mean:

Board() : rows(16)
{
}
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625