【发布时间】: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