2012-10-10 34 views
3

Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?未定义的符号 - 从头文件

显示功能,我知道这个问题得到回答所有的时间,但我一直没能找到我的具体实例的解决方案。这是完整的错误:

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 

我只是想从我的板类拉这个显示功能。这里的主:

#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; 
} 

和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 

和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(); 
     } 
    } 
} 

有关的特定功能我已经提到了这个网站经常寻找答案,但从未亲自使用过它,所以请耐心等待。我非常积极,这很简单,我只是忽略了。

+0

哦,我用G ++在OSX –

+0

是否http://stackoverflow.com/a/12574400/673730帮助吗?即你确定你正在编译'board.cpp'并链接到目标文件吗? (刚注意到你使用的命令行,不,你不是) –

回答

1

应该

g++ main.cpp board.cpp 

你不是编译board.cpp所以符号没有出口。

另外:

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

是错误的。它只是创建一个临时,你大概的意思:

Board() : rows(16) 
{ 
} 
+0

啊是的。我有时候很特别。谢谢。现在要弄清楚为什么它只是seg故障:/ –

+0

@JordanWayneCrabb这是因为'vector (15);'什么也没做。或者使用'rows = vector (16);'而不是16(注意16而不是15)或初始化列表。 –

+0

@JordanWayneCrabb见编辑答案。 –