I'm playing with a simple little inheritance program and I accidentally tried to invoke a constructor that didn't exist and I'm trying to understand the error.
Shape.h
#pragma once
#include "stdafx.h"
#include "string"
#include "iostream"
using namespace std;
enum triangle_type
{
Isoceles,
Scalene,
Equilateral,
Right,
ThreeFourFive
};
class Shape
{
public:
//constructor/destructor
Shape();
Shape(string name);
void Test()
{
enum Color { Red, Blue, Green };
Color color;
int thisThing = Red;
}
Shape* Create(Shape* shape);
virtual string Name();
virtual double Area() = 0;
~Shape();
private:
string name;
triangle_type triangleType;
};
Triangle.h
#pragma once
#include "Shape.h"
#include "string"
class Triangle :
public Shape
{
public:
Triangle();
Triangle(string);
Triangle(int, int);
//Triangle(int); missing constructor
~Triangle();
double Area();
string Name();
private:
int height, base;
string name;
};
Triangle.cpp
#include "stdafx.h"
#include "Triangle.h"
#include "string"
using namespace std;
Triangle::Triangle()
{
}
Triangle::Triangle(string name):
Shape(name)
{
this->name = name;
}
Triangle::Triangle(int newBase, int newHeight)
{
base = newBase;
height = newHeight;
}
Triangle::Triangle(int newBase)
{
base = newBase;
//C2597-you can get an illegal reference to non-static member
//if you try to use a constructor that you don't have defined in your header file
}
double Triangle::Area()
{
return 0.5*(base * height);
}
string Triangle::Name()
{
return "Triangle!";
}
Triangle::~Triangle()
{
}
main.cppp
#include "stdafx.h"
#include "Shape.h"
#include "Triangle.h"
int _tmain(int argc, _TCHAR* argv[])
{
Shape *shape = new Triangle("My triangle");
double area = shape->Area();
Triangle *triangle = new Triangle(3); //error C2664 'Triangle::Triangle(const Triangle&)
//cannot convert argument 1 from 'int' to 'std::string'
shape->Test();
return 0;
}
I recognize that calling the constructor that doesn't exist is why there's an error when I'm calling it, but I have no idea what the error message means. It didn't ring any bells after reading it.