CPP, error, classes, no match for even though the declaration is given

312 views Asked by At

I'm writing a code for my classes and have an error I can't deal with. The errors are as follow. Also I'm not allowed to change anything in the main file:

In file included from lab13.cpp:15:0:
lab13.h: In member function ‘TSeries& TSeries::operator()(int, int)’:
lab13.h:10:39: warning: no return statement in function returning non-void [-Wreturn-type]
         TSeries &operator()(int, int){}
                                       ^
g++ -c -Wall lab13f.cpp
In file included from lab13f.cpp:1:0:
lab13.h:15:10: error: ‘ostream’ in namespace ‘std’ does not name a type
   friend std::ostream &operator<<(std::ostream &o,const TSeries &ts);
          ^
lab13.h: In member function ‘TSeries& TSeries::operator()(int, int)’:
lab13.h:10:39: warning: no return statement in function returning non-void [-Wreturn-type]
         TSeries &operator()(int, int){}
                                       ^
lab13f.cpp: At global scope:
lab13f.cpp:13:9: error: prototype for ‘TSeries TSeries::operator()(int, int)’ does not match any in class ‘TSeries’
 TSeries TSeries::operator()(int, int){}
         ^
In file included from lab13f.cpp:1:0:
lab13.h:10:18: error: candidate is: TSeries& TSeries::operator()(int, int)
         TSeries &operator()(int, int){}
                  ^
lab13f.cpp:16:9: error: prototype for ‘TSeries TSeries::operator+(TSeries&)’ does not match any in class ‘TSeries’
 TSeries TSeries::operator+(TSeries &ts){
         ^
In file included from lab13f.cpp:1:0:
lab13.h:9:17: error: candidate is: const TSeries TSeries::operator+(const TSeries&) const
   const TSeries operator+(const TSeries &ts)const;
                 ^
lab13f.cpp:45:19: error: definition of implicitly-declared ‘TSeries::~TSeries()’
 TSeries::~TSeries(){}
                   ^
lab13f.cpp: In member function ‘TSeries& TSeries::operator=(TSeries (*)(int, int))’:
lab13f.cpp:47:52: warning: no return statement in function returning non-void [-Wreturn-type]
 TSeries &TSeries::operator=(TSeries(int a, int b)){}
                                                    ^
lab13f.cpp: In function ‘std::ostream& operator<<(std::ostream&, const TSeries&)’:

lab13f.cpp:51:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
make: *** [lab13f.o] Błąd 1

I've spend a couple of days trying to solve the issue and gave up finally. Hopefull somebody will help me with my struggle :) I'd be most grateful.


I've forgrotten to add that the main file was written by the tutor and under no ccircumstances I'm not allowed to change ANYTHING in it.

3

There are 3 answers

1
marom On

The compiler is complaining because series1(2,4) invokes TSeries::operator()(int, int), which is missing, and not the constructor TSeries(int, int)

0
Kiril Kirov On

Maybe, it's a typo. This:

TSeries series4=series1(2,4);

does not create series4 with constructor arguments 2 and 4, but instead, it tries to call series1's (NOTE: this is variable) operator()(int, int). And there's no such operator defined.

I guess you need

TSeries series4=TSeries(2,4);

Or even better:

TSeries series4(2,4);

Also, constructions like this:

series1+=1.,0.,3.;

will actually call operator += only once. Just to be clear, as I'm not sure if you expect this. You may read about operator, in C++.
So, if you want 3 additions, you need:

series1+=1.;
series1+=0.;
series1+=3.;

Also note @RSahu's answer about operator=, which I upvoted (nice catch, I didn't notice it).

0
R Sahu On

The line

    TSeries &operator=(TSeries(int a, int b));

declares a function, whose argument is a function that has two inputs of type int and the return type is TSeries.

I don't think you meant to do that.

A copy assignment operator would be declared with the syntax:

    TSeries &operator=(TSeries const& rhs);

Also, it's not clear what you meant in the line:

     TSeries series4=series1(2,4);

Perhaps, you meant to use:

     // Will use the compiler generated copy constructor
     // since you haven't declared one.
     TSeries series4=series1;

or

     // Will use the constructor that takes two ints
     TSeries series4(2,4);