I have three files (see code below), one class in testingMatrix.h
, testingMatrix.cpp
and one mainMatrix.cpp
. I want to use the matrix in the file mainMatrix.cpp
, but it doesn't seem to work, I keep getting an error (I want to combine more files later, but the error is the same for this example);
Undefined symbols for architecture x86_64: "Matrix::Matrix(int, int, double const&)", referenced from: _main in mainMatrix-bd8935.o "Matrix::printMatrix() const", referenced from: _main in mainMatrix-bd8935.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
When I write g++ testingMatrix.cpp
, ./a.out
, I get a correct print. However, when I write g++ mainMatrix.cpp
, ./a.out
, I get the error stated above. I've looked around in the forum but can't seem to find a solution for my particular issue.
The first file;
//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();
}
The second file;
//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;
};
The third file;
//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();
}