c ++真的很混乱链接错误
c++ really confusing linking error
我不断收到这些错误:
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 天了,问了朋友,但似乎没有人有答案
您已经定义矩阵 class 两次 - 一次在 header 中,一次在 .cpp 文件中。您需要 header 中的 class 定义以及 .cpp 文件中的成员函数定义。
1- 在 Matrix class header 中包含 class 字符串,只要您声明一些 class 字符串类型的成员。
2-使headerclass Matrix
只有接口,在Matrix源文件中只定义不再声明class 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];
}
继续像上面那样定义其他成员函数。
我不断收到这些错误:
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 天了,问了朋友,但似乎没有人有答案
您已经定义矩阵 class 两次 - 一次在 header 中,一次在 .cpp 文件中。您需要 header 中的 class 定义以及 .cpp 文件中的成员函数定义。
1- 在 Matrix class header 中包含 class 字符串,只要您声明一些 class 字符串类型的成员。
2-使headerclass Matrix
只有接口,在Matrix源文件中只定义不再声明class 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];
}
继续像上面那样定义其他成员函数。