c ++确实令人困惑的链接错误

时间:2016-12-11 22:32:46

标签: c++ visual-studio compiler-errors header-files

我一直收到这些错误:

cNeuralNetv2.obj : error LNK2019: unresolved external symbol "public: void __thiscall Matrix::set(int,int,double)" (?set@Matrix@@QAEXHHN@Z) referenced in function _wmain

cNeuralNetv2.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Matrix::toStr(void)" (?toStr@Matrix@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _wmain

我的头文件如下所示:

class Matrix{
    private:
        double** mat;
        void makeInitArray();

   public:
       int height, width;
       double at(int, int);
       void set(int, int, double);
       void add(int, int, double);
       string dimensions();
       string toStr();
       Matrix(int h, int w);
};
Matrix dotMatrices(Matrix a, Matrix b);

我的matrix.cpp文件如下所示:

#include "stdafx.h"
#include <vector>
#include <string>
#include <iostream>
#include <stdexcept>
using namespace std;

class Matrix{
private:
    double** mat;

    void makeInitArray(){
        mat = 0;
        mat = new double*[height];
        for (int h = 0; h < height; h++)
        {
            mat[h] = new double[width];
            for (int w = 0; w < width; w++)
            {
                // fill in some initial values
                mat[h][w] = 0.0;
            }
        }
    }

public:
    int height, width;

    double at(int x, int y){
        return mat[x][y];
    }

    void set(int x, int y, double z){
        mat[x][y] = z;
    }

    void add(int x, int y, double z){
        mat[x][y] = z;
    }

    string toStr(){
        string output = "";
        for (int x = 0; x < height; x++){
            output += "[ ";
            for (int y = 0; y < width; y++){
                output += to_string(mat[x][y]);
                output += " ";
            }
            output += "]\n";
        }
        return output;
    }

    string dimensions(){
        return to_string(height) + "x" + to_string(width);
    }

    Matrix(int h, int w){
        height = h;
        width = w;
        makeInitArray();
    }
};

我的主文件如下所示:

#include "stdafx.h"
#include <vector>
#include <string>
#include <iostream>
#include <stdexcept>

using namespace std;

#include "mat.h"

int _tmain(int argc, _TCHAR* argv[])
{
    Matrix nMat(4, 3);
    cout << nMat.toStr() << endl;
    cout << nMat.dimensions() << endl;
    nMat.add(0, 0, 1);
    nMat.at(0, 0);
    nMat.set(0, 0, 2);
    int q;
    cin >> q;
    return 0;
}

我是c ++的菜鸟,但我已经看了4天了,问朋友,似乎没有人有答案

2 个答案:

答案 0 :(得分:3)

您已经定义了Matrix类两次 - 一次在标题中,一次在.cpp文件中。您需要标头中的类定义以及.cpp文件中的成员函数定义。

答案 1 :(得分:1)

只要声明类型为class string的某些成员,

1-在Matrix类头中包含类字符串。

2-使标题class Matrix仅具有接口,并且在Matrix源文件中仅定义不再声明类Matrix。

您的Matrix.h将如下所示:

#include <string> // don't forget

class Matrix
{
    private:
        double** mat;
        int height, width; // declare member data private or protected an provide setters and getters instead of declaring them public
        void makeInitArray();

   public:          
       void add(int, int, double);
       // ...
};

和Matrix来源Matrix.cpp将如下所示:

#include "matrix.h"
// some other include headers here...

void Matrix::makeInitArray() // don't write: void makeInitArray() directly
{
    mat = 0;
    mat = new double*[height];
    for (int h = 0; h < height; h++)
    {
        mat[h] = new double[width];
        for (int w = 0; w < width; w++)
        {
            // fill in some initial values
            mat[h][w] = 0.0;
        }
    }
}

double Matrix::at(int x, int y)
{
    return mat[x][y];
}

继续其他会员功能&#39;像上面这样的定义。