错误:尝试创建新对象时出现“未定义的引用”

时间:2019-09-27 23:16:28

标签: c++

当尝试使用g ++编译器运行代码时,出现此错误“ undefined reference to ...”。我认为这可能与在main.cpp文件中创建board类的对象有关。请帮助我无法找出导致此错误消息的原因。我浏览了其他类似错误消息的帖子,但找不到我的问题的答案。我的问题不是重复,因为这涉及创建新对象:

错误消息:

undefined reference to `board::board(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
main.cpp:(.text+0x258): undefined reference to `board::board(int, int)'
collect2: error: ld returned 1 exit status

这是我的文件:

board.h:

#include <iostream>

using namespace std;

class board{
    public:
        board();
        board(int row, int columns);
        board(string filename);
        ~board();


};

board.cpp:

#include <iostream>
#include <fstream>
#include "board.h"

using namespace std;

//*char boardArray; 
board::board() 
{

}
board::board(string filename)
{
    ifstream mapFile;
    mapFile.open(filename);
}

board::board(int row, int column)
{

}

board::~board()
{

}

main.cpp:

#include <string.h>
#include <fstream>
#include "board.h"

using namespace std;

int main(int argc, char **argv)
{
    int input;

    cout << "1) Read map file \n2) Generate random board" << endl;
    cin >> input;
    if (input == 1)
    {
        string filename;
        cout << "what is the name of the file?" << endl;
        cin >> filename;
        board *b1 = new board(filename);
    }
    else if (input == 2)
    {
        string input = "";
        cout << "what are the dimensions of the board? (ex. 5x5)" << endl;
        cin >> input;
        int delimPos = input.find("x");
        int row = stoi(input.substr(0,delimPos));
        int column = stoi(input.substr(delimPos + 1));
        board *b1 = new board(row, column);
        //cout << "Row: " << row << " Column: " << column << endl;
    }

}
'''

0 个答案:

没有答案