[尝试从另一个文件运行时出现g ++错误

问题描述 投票:0回答:1

我有三个文件(请参见下面的代码),testingMatrix.htestingMatrix.cpp中的一个类和mainMatrix.cpp中的一个类。我想在文件mainMatrix.cpp中使用矩阵,但似乎不起作用,我不断收到错误消息(我想稍后合并更多文件,但此示例中的错误相同);

体系结构x86_64的未定义符号:“ Matrix :: Matrix(int,int,double const&)”,引用自:_main在mainMatrix-bd8935.o中从以下位置引用的“ Matrix :: printMatrix()const”_main在mainMatrix-bd8935.o中ld:找不到架构x86_64的符号clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)

[当我写g++ testingMatrix.cpp./a.out时,打印正确。但是,当我写g++ mainMatrix.cpp./a.out时,出现上述错误。我在论坛上四处张望,但似乎找不到针对我特定问题的解决方案。

第一个文件;

//mainMatrix.cpp
#include "testingMatrix.h"

int main(){
    const double value = 3.0;
    int rows = 3;
    int columns = 3;
    Matrix testing(rows, columns, value);
    testing.printMatrix();
}

第二个文件;

//testingMatrix.h
#include <iostream>
#include <vector>

class Matrix{
    private:
        int the_rows;
        int the_columns;
        std::vector<std::vector<double> > the_matrix;

    public:
        Matrix(int rows, int cols, const double& value);
        void printMatrix() const;
};

第三文件;

//testingMatrix.cpp
#include "testingMatrix.h"
#include <vector>

Matrix::Matrix(int rows, int cols, const double& value){
    the_rows = rows;
    the_columns = cols;
    the_matrix.resize(the_rows);
    for (int i = 0; i < the_matrix.size(); i++){
        the_matrix.at(i).resize(the_columns, value);
    }
}

void Matrix::printMatrix() const{
    std::cout << "Matrix: " << std::endl;
    for (unsigned i = 0; i < the_rows; i++) {
        std::cout << "[";
        for (unsigned j = 0; j < the_columns; j++) {
            if (j==the_columns-1){
                std::cout << the_matrix.at(i).at(j);
            }
            else{
                 std::cout << the_matrix.at(i).at(j) << ", ";
            }
        }
        std::cout << "]" << std::endl;
    }
}

int main(){
    Matrix testing(3, 3, 3.0);
    testing.printMatrix();
}
c++ clang
1个回答
0
投票

您需要编译链接您的源代码...

g++ -o mainMatrix testMatrix.cpp mainMatrix.cpp

g++ -c testMatrix.cpp
g++ -c mainMatrix.cpp
g++ -o mainMatrix testMatrix.o mainMatrix.o
© www.soinside.com 2019 - 2024. All rights reserved.