0

I have two classes: a class Complex and a class Matrix.

Isn't my constructor supposed to substitute the void arguments constructor as well? It throws an error util I declare Complex() constructor as well. g++ -std=c++14

Complex.h

class Complex { private: int m_real, m_imaginary; public: Complex(const int, const int); } 

complex.cpp

#include "Complex.h" // Constructor Complex::Complex(const int real = 0, const int img = 0) : m_real(real), m_imaginary(img) { } 

Matrix.h

class Complex; class Matrix { private: int m_lines, m_columns; Complex *m_matrix; public: Matrix(const int, const int, const Complex &); } 

matrix.cpp

#include "Matrix.h" #include "Complex.h" Matrix::Matrix(const int nr_lines, const int nr_columns, const Complex &comp) : m_lines(nr_lines), m_columns(nr_columns) { m_matrix = new Complex[nr_lines * nr_columns]; some other code goes here... 

|7|error: no matching function for call to 'Complex::Complex()'|

12
  • 4
    Can you please try to create a Minimal, Complete, and Verifiable Example and show us? Together with the actual errors you get (in full, complete and unedited)? Commented Mar 14, 2017 at 11:51
  • 1
    Consider using standard types. std::vector<std::vector<std::complex<int>>> should suffice. Commented Mar 14, 2017 at 12:00
  • 2
    Are you sure that you have default values for real and img in your code? If there wasn't any default values then new Complex[n] would have thrown error without Complex(). Commented Mar 14, 2017 at 12:11
  • 3
    The problem is that you set the constructor default argument values when you define the constructor, not when you declare it. It needs to be done for the declaration. Commented Mar 14, 2017 at 12:25
  • 1
    It's only needed in the declaration. Remember that source files are really independent from each other (please learn more about translation units) so all the compiler knows is what it sees in the current source file, which is the declaration. Commented Mar 14, 2017 at 12:38

2 Answers 2

2

Same here - I tested the code I wrote based on your description. It compiles and runs just fine on VS2015, VS2017.

class Complex { private: int m_real; int m_img; public: Complex(const int real = 0, const int img = 0) : m_real(real) , m_img(img) { } }; class Matrix { private: Complex* matrix; public: Matrix(int nr_lines = 3, int nr_columns = 3) { matrix = new Complex[nr_lines * nr_columns]; } ~Matrix() { delete[] matrix; } }; int main() { Matrix* t = new Matrix(); return 1; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Your problem is in the declaration of the Complex constructor, in complex.h file. There, you didn't declare the default values. Copy the signature of the constructor from complex.cpp, and it should work
1

It seems your error is somewhere else. As Some programmer dude pointed out, you could have figured this out with a minimal, complete and verifiable example - https://stackoverflow.com/help/mcve

I created a small example:

#include <iostream> #include <stdlib.h> class Complex { private: int m_n; int m_i; public: Complex (const int n = 0, const int i = 0) : m_n (n), m_i (i) { std::cout << "Complex ctor: " << n << ", " << i << std::endl; }; }; int main(int argc, char** argv) { int cnt = 12; if (argc > 1) cnt = atoi (argv[1]); Complex* m = new Complex[cnt]; (void)m; //no warning for unused variable return 0; } 

Building with g++ and running:

pan:~$ g++ example.cpp -Wall -o example.elf pan:~$ ./example.elf 4 Complex ctor: 0, 0 Complex ctor: 0, 0 Complex ctor: 0, 0 Complex ctor: 0, 0 pan:~$ 

As you can see, this C++ class constructor works well & as expected.

My gcc is g++ (SUSE Linux) 4.8.5.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.