【问题标题】:c++ really confusing linking errorc ++真正令人困惑的链接错误
【发布时间】:2016-12-11 22:32:45
【问题描述】:

我不断收到这些错误:

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 天了,问了朋友,似乎没有人有答案

【问题讨论】:

  • 你是否在头文件中包含了string
  • 您的 matrix.cpp 文件有误,它声明了另一个名为 Matrix 的不同类,这是未定义的行为(违反 ODR)。
  • 您没有在头文件和源文件中声明该类。您在 cpp 中使用 Matrix:: 语法来编写实现。

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    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.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];
    }
    

    继续上述其他成员函数的定义。

    【讨论】:

    • 寻找 std::to_string()。它在字符串头文件中定义。它是 std::stoi 或 atoi 的别名
    猜你喜欢
    • 2014-10-11
    • 2016-03-24
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 2012-09-12
    • 1970-01-01
    相关资源
    最近更新 更多