编译错误,据我所知不是错字

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

本文关键字:据我所知 错误 编译      更新时间:2023-10-16
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;
    }
};

我有这个错误:

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

怎么了?

另外,这是整个代码:

#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()
    {
        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;
}
这是一个

错字。您只在函数中的最后一个")"之前编写了<。有些事情只是更容易发现格式正确的代码:

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

大量"<<"运算符中缺少一个"<":

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

应该是:

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

这个故事的寓意是布置你的代码,以便很容易看到规律性(和不规则性)。 将所有内容压缩到一行上会使阅读变得困难 - 因此很难发现错误。 在二进制运算符周围使用空格会有所帮助。

注意到 Rob 指出的过度使用endl,我会考虑写:

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

或者甚至可以将所有坐标全部保留在一条线上:

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

还有几点:

  • 你不需要#include <stdio.h>#include <conio.h>来获取原始代码进行编译(或者,至少,我不需要在带有GCC/G ++ 4.6.0的MacOS X 10.6.7上)。 我只是简单地注释掉了这些行。

  • 你提到scanf()有问题. 通常不应使用一般的<stdio.h>scanf()系列函数,尤其是在使用C++编码时。

<<y3<")"<<endl;

应该是

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

顺便说一句,我的一条规则是:当你的意思是'n'时,永远不要说endlstd::endl不仅仅是结束一行。它还会将缓冲的输出数据刷新到操作系统,这可能意味着每次调用时都会进行昂贵的系统调用。

试试这个:

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

这是因为您在此行y3后只有一个<

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

就像其他人说的那样,它有助于很好地格式化您的代码。 代码片段的可读性更强的形式可能如下所示:

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;
    }
};
这是一个

错字:

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