0

I have a collect2 error (as in the title) when I try to test this code to get the inverse or print the matrix. Any suggestions ? Thank you

So here's my header file and below you can find the implementation file and the main function

#ifndef SUBMISSION_MATRIX2X2_HPP_
#define SUBMISSION_MATRIX2X2_HPP_

#include <iostream>


class Matrix2x2 {

private:

    double val00; // first row, first column
    double val01; // first row, second column
    double val10; // second row, first colum
    double val11; // second row, second column

public:
    Matrix2x2();
    Matrix2x2(const Matrix2x2& other);
    Matrix2x2(double a, double b, double c, double d);
    double CalcDeterminant() const;
    Matrix2x2 CalcInverse() const;

    Matrix2x2& operator=(const Matrix2x2& z);
    Matrix2x2 operator-() const;
    Matrix2x2 operator+(const Matrix2x2& z) const;
    Matrix2x2 operator-(const Matrix2x2& z) const;
    void Print() const;
    };
    #endif /* SUBMISSION_MATRIX2X2_HPP_ */

here's the implementation code:

#include "header.hpp"
#include <iostream>
//Constructor setting everything at 0
Matrix2x2::Matrix2x2()
{
    val00 = 0.0;
    val01 = 0.0;
     val10 = 0.0;
    val11 = 0.0;
 }
 //Overriden copy constructor
Matrix2x2::Matrix2x2(const Matrix2x2& other)
{
    val00 = other.val00;
    val01 = other.val01;
    val10 = other.val10;
     val11 = other.val11;
 }
 //filling a,b,c,d constructor
 Matrix2x2::Matrix2x2(double a, double b, double c, double d)
 {
    val00 = a;
    val01 = b;
    val10 = c;
    val11 = d;
}
//printing the matrix
 void Matrix2x2::Print() const
{
    std::cout << "{" <<"\n";
    std::cout << val00 << "," << val01 << "\n" endl;
    std::cout << val10 << "," << val11 << "\n" endl;
    std::cout << "}" << endl;
}
//determinant calculator
double Matrix2x2::CalcDeterminant() const
{
    return (val00 * val11 - val01 * val10);
}
//inverse calculator
Matrix2x2 Matrix2x2::CalcInverse() const
{
    double det = CalcDeterminant();
    Matrix2x2 M(val11 / det, -val10 / det, -val01 / det,    val00 / det);
    return M;
}

main function: #include #include "Matrix2x2.hpp"

int main(int argc, char* argv[]){
 Matrix2x2 M(2, 3, -1, 0);

    std::cout <<"Det M=" << M.CalcDeterminant() <<"\n";
    M.CalcInverse();
    M.Print();
    return 0;
}
Manuella
  • 1
  • 2
  • Undefined symbols for architecture arm64: "__ZNK9Matrix2x25PrintEv", referenced from: _main in ccy1Dtu5.o ld: symbol(s) not found for architecture arm64 – Manuella May 24 '22 at 13:01
  • 1
    Maybe `"\n" << endl` instead of your `"\n" endl` ? – Picaud Vincent May 24 '22 at 13:01
  • 2
    I do not see a program. I see a class, but no `main` entry point to show there is a program to run. – PaulMcKenzie May 24 '22 at 13:02
  • I was trying to test this: int main(int argc, char* argv[]){ Matrix2x2 M(2, 3, -1, 0); std::cout <<"Det M=" << M.CalcDeterminant() <<"\n"; M.CalcInverse(); M.Print(); return 0; } – Manuella May 24 '22 at 13:06
  • `//Overriden copy constructor` -- This is totally unnecessary, given the members. All you will accomplish is to possibly introduce bugs, and if not that, provide a less efficient copy than what the compiler's default will give you. Get rid of it. – PaulMcKenzie May 24 '22 at 13:06
  • Since I see this: `cout << "{"` without `using namespace std;` anywhere, I am assuming you are using `using namespace std;` within the header file. This [should not be done](https://stackoverflow.com/questions/4872373/why-is-including-using-namespace-into-a-header-file-a-bad-idea-in-c). Also, please post `header.h`. – PaulMcKenzie May 24 '22 at 13:12
  • 1
    @Manuella - You've only provided partial code, so people have to guess what your actual problem is. But that error means an attempt to either call a function or use a named global that hasn't actually being defined. The name will be mangled (encoded) in a compiler-specific way, but will have some relationship to the missing name in your code. If you're not going to provide a [mcve], you're going to need to work out for yourself what is missing. – Peter May 24 '22 at 13:23
  • sorry guys, I have just edited the code and added the header and the main function. Thanks for your help – Manuella May 24 '22 at 13:34
  • Is the error the only undefined symbol? Perhaps you are using VSCode and did not edit your tasks.json so it's only building the active file and ignoring the other cpp file. That is the default VSCode behavior but the documentation tells you how to support more than 1 source file. – drescherjm May 24 '22 at 13:56
  • yes this is the error, and I am not using VSCode. I am working on a text editor + using the terminal to compile – Manuella May 24 '22 at 13:57
  • Can you add the text of the entire build output from the terminal? – drescherjm May 24 '22 at 14:01
  • Undefined symbols for architecture arm64: "__ZNK9Matrix2x211CalcInverseEv", referenced from: _main in ccgB97eq.o "__ZNK9Matrix2x25PrintEv", referenced from: _main in ccgB97eq.o ld: symbol(s) not found for architecture arm64 collect2: error: ld returned 1 exit status – Manuella May 24 '22 at 14:03
  • 1
    @Manuella *using the terminal to compile* -- What is the command you are using to build your program? Are you *linking* all the modules? – PaulMcKenzie May 24 '22 at 14:05
  • g++ -Wall -lm -O -o Matrix2x2test Matrix2x2test.cpp Matrix2x2.o /// This worked for the CalcDeterminant, it doesn't for the CalcInverse – Manuella May 24 '22 at 14:08
  • Maybe `Matrix2x2.o` is out of date meaning `Matrix2x2.cpp` needs to be recompiled. – drescherjm May 24 '22 at 14:09

0 Answers0