1

I've created very simple C++ project in codeblocks. I have header file (CVector.h), source file (CVector.cpp) and main code (code.cpp). When I try to compile the code I got the following message:

CVector.cpp|22|error: no matching function for call to 'CVector::CVector()'|

code.cpp

#include <iostream>
#include "CVector.h"
using namespace std;

int main () {
CVector vec1(1,2);
CVector vec2 (3,4);
cout << "vec1 data "; vec1.printData();
cout << "vec2 data "; vec2.printData();

cout << "vec1 area: " << vec1.area() << endl;
cout << "vec2 area: " << vec2.area() << endl;

return 0;
}

CVector.h

#ifndef CVECTOR_H
#define CVECTOR_H


class CVector
{
   int x,y;
   public:
        CVector (int, int);
        int area();
        void printData ();
        CVector operator+ (CVector param );
};

#endif // CVECTOR_H

CVector.cpp

#include <iostream>
#include "CVector.h"
using namespace std;

CVector::CVector (int a, int b) {
x=a;
y=b;
}

int CVector::area() {return x*y;}

void CVector::printData(){
cout << "X = " << x << ", Y = " << y << endl;
}

CVector CVector::operator+ (CVector param )
{
    CVector temp;
    temp.x=x + param.x;
    temp.y=y + param.y;
    return temp;
}

The error is related to the operator overloading function because it compiles without problems when I comment this function.

Mohamed Ouda
  • 121
  • 2
  • 1
    The [default constructor is not auto generated when you explicitly declare you're own](http://stackoverflow.com/questions/3734247/what-are-all-the-member-functions-created-by-compiler-for-a-class-does-that-hap), as you've done (`CVector(int, int)`). – James Adkison Jun 17 '16 at 13:40
  • Yes I know, I just noticed that I need it in the definition of the operator overloading. Thank you so much – Mohamed Ouda Jun 17 '16 at 13:53
  • Edit: ... when you explicitly declare **your** own ... – James Adkison Jun 17 '16 at 13:53

2 Answers2

2

In operator+, the line

CVector temp;

needs a default constructor, but you didn't define one.
(If you define a non-default constructor, the compiler will not provide a default one.)

You can either define a default constructor, if you can decide on what suitable default values of the members could be, or you can write

CVector CVector::operator+ (CVector param )
{
    CVector temp(x + param.x, y + param.y);
    return temp;
}

or even

CVector CVector::operator+ (CVector param )
{
    return CVector(x + param.x, y + param.y);
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

The problem in you operator + is this line:

CVector temp;

This lines creates a default-constructed CVector. However, your CVector class does not have a default constructor, hence the error. You need to add one to the class:

class CVector
{
   int x,y;
   public:
      CVector(); // Added
      //The rest as before
};

There are two ways to implement it. One:

CVector::CVector() {}

This will result in the members x and y remaining uninitialised.

Two:

CVector::CVector() : x(0), y(0) {}

This will initialise x and y to 0.

The second one is safer, the first one is faster. Which one to use depends on your goals for the class.


Unrelated, but in your two-parameter constructor, you should use the member initialisation list instead of assignments as well:

CVector::CVector(int a, int b) : x(a), y(b) {}

It doesn't really matter for primitive types such as int, but it's a good habit to grow. It's more efficient for members of a type with a nontrivial default constructor, and is actually nceessary for members of a type without a default constructor.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455