开发者

Error at compiling, not a typo as far as I see

class Triangulo: public Figura
{
    public:
    int x1, y1, x2, y2, x3, y3;
    void dibujar_triangulo()
    {
        cout<<"Se ha dibujado un triangulo color "<<color<<" con coordenadas "<<
        "("<<x1<<","<<y1<<")"<<endl<<"("<<x2<<","<<y2<<")"<<endl<<"("<<x3<<","<<y3<")"<<endl;
    }
};

I've got this error:

invalid operands of types 'const char [2]' and '<unresolved overloaded function type>' to binary 'operator<<'

What's wrong?

Also, Here's the whole code:

#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

class Figura
{
    public:
    string color, nombre;
    int num_lados;
    void establecer_color(string param)
    {
        color=param;
    }
    string obtener_color()
    {
        string a;
        cin>>a;
        return a;
    }
    void establecer_lados(int param)
    {
        num_lados=param;
    }
    int obtener_lados()
    {
        int a;
        cin>>a;
        return a;
    }
};

class Circulo: public Figura
{
    public:
    int x, y;
    void dibujar_circulo()
    {
        cout<<"Se ha dibujado un circulo color "<<color<<" con centro en ("<<x<<","<<y<<")"<<endl;
    }
};

class Rectangulo: public Figura
{
    public:
    int x1, y1, x2, y2;
    void dibujar_rectangulo()开发者_JS百科
    {
        cout<<"Se ha dibujado un rectangulo color "<<color<<" con coordenadas "
        <<"("<<x1<<","<<y1<<")"<<endl<<"("<<x2<<","<<y2<<")"<<endl;
    }
};

class Triangulo: public Figura
{
    public:
    int x1, y1, x2, y2, x3, y3;
    void dibujar_triangulo()
    {
        cout<<"Se ha dibujado un triangulo color "<<color<<" con coordenadas "<<
        "("<<x1<<","<<y1<<")"<<endl<<"("<<x2<<","<<y2<<")"<<endl<<"("<<x3<<","<<y3<")"<<endl;
    }
};

int main()
{
    Circulo Cir;
    Triangulo Tri;
    Rectangulo Rec;
    int i;
    initsw:
    cout<<"Eliga:"<<endl<<"1.\tCirculo"<<endl<<"2.\tRectangulo"
    <<endl<<"3.\tTriangulo"<<endl<<"4.\tSalir"<<endl;
    cin>>i;
    cout<<"Indique el color de su figura"<<endl;
    cin>>Cir.color;
    Tri.color=Cir.color;
    Rec.color=Cir.color;
    switch (i)
    {
        case 1:
        cout<<"Por favor introduzca el centro de su circulo:(x,y)"<<endl;
        scanf("(%d,%d)",&(Cir.x),&(Cir.y));
        Cir.dibujar_circulo();
        break;
        case 2:
        cout<<"Por favor introduzca las coordenadas de su rectangulo:(x1,y1),(x2,y2)"<<endl;
        scanf("(%d,%d),(%d,%d)",&(Rec.x1),&(Rec.y1),&(Rec.x2),&(Rec.y2));
        Rec.dibujar_rectangulo();
        break;
        case 3:
        cout<<"Por favor introduzca las coordenadas de su triangulo:(x1,y1),(x2,y2),(x3,y3)"<<endl;
        scanf("(%d,%d),(%d,%d),(%d,%d)",&(Tri.x1),&(Tri.y1),&(Tri.x2),&(Tri.y2),&(Tri.x3),&(Tri.y3));
        Tri.dibujar_triangulo();
        break;
        case 4:
        goto end;
        break;
        default:
        cout<<"Error, elija otra opcion"<<endl;
        goto initsw;
    }
    end:
    return 0;
}


It is a typo. You only wrote < before the last ")" in your function. Some things are just easier to spot with properly formatted code:

void dibujar_triangulo()
{
    cout << "Se ha dibujado un triangulo color " << color << " con coordenadas "
         << "(" << x1 << "," << y1 << ")" << endl
         << "(" << x2 << "," << y2 << ") "<< endl
         << "(" << x3 << "," << y3 < ")" << endl;
//                                 ^^ should be <<
}


You are missing one '<' from your large collection of '<<' operators:

void dibujar_triangulo()
{
    cout << "Se ha dibujado un triangulo color " << color << " con coordenadas " <<
    "(" << x1 << "," << y1 << ")" << endl <<
    "(" << x2 << "," << y2 << ")" << endl <<
    "(" << x3 << "," << y3<")" << endl;
}

Should be:

    "(" << x3 << "," << y3 << ")" << endl;

The moral of the story is to lay out your code so that the regularities (and irregularities) can be seen easily. Squeezing everything onto one line makes it hard to read - and therefore to spot the errors. Using spaces around binary operators helps.

Noting the over-use of endl that Rob pointed out, I'd consider writing:

void dibujar_triangulo()
{
    cout << "Se ha dibujado un triangulo color " << color << " con coordenadas " <<
        "(" << x1 << "," << y1 << ")\n" <<
        "(" << x2 << "," << y2 << ")\n" <<
        "(" << x3 << "," << y3 << ")"   << endl;
}

Or maybe even keep the coordinates all on a single line:

void dibujar_triangulo()
{
    cout << "Se ha dibujado un triangulo color " << color << " con coordenadas\n" <<
        "(" << x1 << "," << y1 << "), " <<
        "(" << x2 << "," << y2 << "), " <<
        "(" << x3 << "," << y3 << ")"   << endl;
}

A couple more points:

  • You don't need either #include <stdio.h> or #include <conio.h> to get the original code to compile (or, at least, I don't on MacOS X 10.6.7 with GCC/G++ 4.6.0). I simply commented out those lines.

  • You mention having problems with scanf(). You should not usually use <stdio.h> in general or the scanf() family of functions in particular when you code in C++.


<<y3<")"<<endl;

should have been

<<y3<<")"<<endl;

As an aside, one of my rules is: never say endl when you mean '\n'. std::endl does more than just end a line. It also flushes the buffered output data to the OS, which can mean an expensive system call every time it is invoked.

Try this:

cout<<"Se ha dibujado un triangulo color "
      <<color<<" con coordenadas "
      << "("
      <<x1<<","<<y1
      <<")\n("
      <<x2<<","<<y2
      <<")\n("
      <<x3<<","<<y3
      <<")\n";


It's because you only have one < after y3 on this line:

"("<<x1<<","<<y1<<")"<<endl<<"("<<x2<<","<<y2<<")"<<endl<<"("<<x3<<","<<y3<")"<<endl;
                                                     //The error is here. ^

Like others have said, it helps to format your code well. A more readable form of your code snippet might look like this:

class Triangulo: public Figura
{
    public:

    int x1, y1, x2, y2, x3, y3;

    void dibujar_triangulo()
    {
        cout << "Se ha dibujado un triangulo color " << color<< " con coordenadas "
             << "(" << x1 << "," << y1 << ")" << endl
             << "(" << x2 << "," << y2 << ")" << endl
             << "(" << x3 << "," << y3 << ")" << endl;
    }
};


It is a typo:

... <<y3<")"<<endl;
//      ^ error here, < instead of <<
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜