-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,461
  • 33
  • 112
  • 197
  • 2
    Why do so many people treat C++ as if it's Java? You did not `delete` any of the memory you allocated, and even if you tried to, your program invokes undefined behavior due to `Shape` not having a virtual destructor. – PaulMcKenzie Jan 13 '15 at 03:55
  • Also, `TCHAR` is either an indicator of an ongoing conversion from ancient pre-NT windows to more modern systems, or a code-smell. There's exactly one arguably valid modern use: Condensing documentation. – Deduplicator Jan 13 '15 at 04:10
  • @Deduplicator care to elucidate? – wootscootinboogie Jan 13 '15 at 04:17
  • See http://stackoverflow.com/questions/234365/is-tchar-still-relevant and disregard the accepted answer (or read it to find the logical flaws). – Deduplicator Jan 13 '15 at 04:22

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
  • 286,269
  • 29
  • 621
  • 977
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,308
  • 1
  • 18
  • 23