<分区>
当我在编译代码时遇到困难时,我正在使用我的 mbp 在线练习随机问题。 我确实在网上搜索了类似的问题,但这是我想出的,但仍然无法编译。
我有错误信息
c++ test.cpp -o test
Undefined symbols for architecture x86_64:
"matrix::mSubtraction(float*, float*)", referenced from:
_main in test-fb9462.o "matrix::mMultiplication(float*, float*)", referenced from:
_main in test-fb9462.o "matrix::mShow(float*)", referenced from:
_main in test-fb9462.o "matrix::mAddition(float*, float*)", referenced from:
_main in test-fb9462.o "matrix::mDivision(float*, float*)", referenced from:
_main in test-fb9462.o
ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test] Error 1
测试.cpp
#include <iostream>
#include "matrix.h"
using namespace std;
int main()
{
float matrix_a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
float matrix_b[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
matrix m;
m.Cols = 3;
m.Rows = 3;
cout << "Matrix A :\n";
m.mShow((float *)matrix_a);
cout << "Matrix B :\n";
m.mShow((float *)matrix_b);
cout << "Matrix Addition :\n";
m.mAddition((float *)matrix_a,(float *)matrix_b);
m.mShow((float *)m.ans);
cout << "Matrix Subtraction :\n";
m.mSubtraction((float *)matrix_a,(float *)matrix_b);
m.mShow((float *)m.ans);
cout << "Matrix Multiplication :\n";
m.mMultiplication((float *)matrix_a,(float *)matrix_b);
m.mShow((float *)m.ans);
cout << "Matrix Division :\n";
m.mDivision((float *)matrix_a,(float *)matrix_b);
m.mShow((float *)m.ans);
return 0;
}
矩阵.cpp
#include <iostream>
void matrix::mShow(float *A) {
for(int i = 0; i < matrix::Cols; i++) {
for (int j = 0; j < matrix::Rows; j++) {
printf("%f \n",*((A+i*3)+j));
}
}
}
void matrix::mAddition(float *A, float *B) {
for(int i = 0; i < matrix::Cols; i++) {
for (int j = 0; j < matrix::Rows; j++) {
matrix::ans[i][j]= *((A+i*3) + j) + *((B+i*3) + j);
}
}
}
void matrix::mSubtraction(float *A, float *B) {
cout << "After matrix addition" << endl;
for(int i = 0; i < matrix::Cols; i++) {
for (int j = 0; j < matrix::Rows; j++) {
matrix::ans[i][j]= *((A+i*3) + j) - *((B+i*3) + j);
}
}
}
void matrix::mMultiplication(float *A, float *B) {
cout << "After matrix addition" << endl;
for(int i = 0; i < matrix::Cols; i++) {
for (int j = 0; j < matrix::Rows; j++) {
matrix::ans[i][j]= *((A+i*3) + j) * *((B+i*3) + j);
}
}
}
void matrix::mDivision(float *A, float *B) {
cout << "After matrix addition" << endl;
for(int i = 0; i < matrix::Cols; i++) {
for (int j = 0; j < matrix::Rows; j++) {
matrix::ans[i][j]= *((A+i*3) + j) / *((B+i*3) + j);
}
}
}
矩阵.h
#ifndef __MATRIX_H__
#define __MATRIX_H__
#include <iostream>
using namespace std;
class matrix {
private:
public:
int Cols;
int Rows;
float ans[3][3];
void mShow(float *);
void mAddition(float *,float *);
void mSubtraction(float *,float *);
void mMultiplication(float *,float *);
void mDivision(float *,float *a);
};
#endif
谢谢你们好心地帮助我。