-1

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.

wootscootinboogie
  • 8,056
  • 33
  • 105
  • 186

2 Answers2

4

Pretty straightforward error:

cannot convert argument 1 from 'int' to 'std::string'

You have one constructor that takes one argument:

Triangle(string);

So that is the complete overload set that is considered. You are calling it with an int:

Triangle *triangle = new Triangle(3);

There is no other constructor to consider, so the compiler will try all implicit conversions at its disposal - all of which fail. Hence, the compile error.

Barry
  • 267,863
  • 28
  • 545
  • 906
3

The compiler will, where possible, convert between types to make a function call succeed. In your case, you have a constructor that takes one argument (a std::string) and so the compiler tries to convert the int you gave to a std::string which it expects. It can't, which is why you get that particular error.

Phil
  • 2,278
  • 1
  • 17
  • 23