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;
}
答案 0 :(得分:6)
这是一个错字。您只在函数中的最后一个<
之前写了")"
。使用格式正确的代码可以更容易地找到一些东西:
void dibujar_triangulo()
{
cout << "Se ha dibujado un triangulo color " << color << " con coordenadas "
<< "(" << x1 << "," << y1 << ")" << endl
<< "(" << x2 << "," << y2 << ") "<< endl
<< "(" << x3 << "," << y3 < ")" << endl;
// ^^ should be <<
}
答案 1 :(得分:2)
你错过了一个'&lt;'来自您的大量'&lt;&lt;&lt;操作符:
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 coordenadas\n" <<
"(" << x1 << "," << y1 << "), " <<
"(" << x2 << "," << y2 << "), " <<
"(" << x3 << "," << y3 << ")" << endl;
}
还有几点:
您不需要#include <stdio.h>
或#include <conio.h>
来编译原始代码(或者,至少,我不会在MacOS X 10.6.7上使用GCC / G ++ 4.6.0)。我只是评论了这些内容。
您提到scanf()
有问题。在C ++中编码时,通常不应使用<stdio.h>
或scanf()
系列函数。
答案 2 :(得分:1)
<<y3<")"<<endl;
应该是
<<y3<<")"<<endl;
顺便说一句,我的一条规则是:当你的意思是endl
时,永远不要说'\n'
。 std::endl
不只是结束一条线。它还将缓冲的输出数据刷新到操作系统,这可能意味着每次调用时都会进行昂贵的系统调用。
试试这个:
cout<<"Se ha dibujado un triangulo color "
<<color<<" con coordenadas "
<< "("
<<x1<<","<<y1
<<")\n("
<<x2<<","<<y2
<<")\n("
<<x3<<","<<y3
<<")\n";
答案 3 :(得分:1)
这是因为此行<
之后只有一个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;
}
};
答案 4 :(得分:1)
这是一个错字:
... <<y3<")"<<endl;
// ^ error here, < instead of <<