编译错误,据我所知不是错字
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;
}
};
我有这个错误:
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'
时,永远不要说endl
。 std::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 <<
相关文章:
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- 为什么我遇到分段错误?
- 为什么我的 heap.h 文件给我一个LNK2001错误?
- C++无法解决我的问题。我有控制台错误
- 我包含了 std,但我仍然收到错误,这是为什么?
- C++:使用类在向量中搜索特定元素时,我得到了错误的结果
- 您好,我是C++初学者,有这种问题.它给了我倍增的错误
- 我编写了以下代码来读取C++矩阵,然后打印其行和列.我收到此错误
- 我收到分段错误,但不了解分段错误发生的位置
- 我不断收到错误 C2440'=':无法从"int *(__cdecl *)(int *,int *,int,int)"转换为"int *
- 我一直收到错误"cannot convert 'float*' to 'float' in return"
- 发出给我的理解错误消息(启动C++/编程)
- 使用已删除的指针地址.据我所知,
- Microsoft Visual Studio发现我所有的CUDA设备端函数都有错误
- 特征 3 断言在求解线性系统时失败 - 据我所知,这是由于特征中的无效索引
- C 中对象方法的地址.据我所知,
- 据我所知,根据 C++14 中的 §5.19/3 和 §5.19/2 编写,此代码不应编译
- 据我所知,下面的函数不是constexpr,而是用clang和g++编译的代码.我错过了什么
- 编译错误,据我所知不是错字