#include<iostream>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include <complex>
#include<windows.h>
#define PI 3.14159265358979323846
#define A 0.0000000001
using namespace std;
complex<double> dir(0,1);
class Car
{
directionX=0;
directionY=1;
public:
Car(char *type)
{
fuel=30;
speed=0;
}
void TurnLeft()
{
const double angle = 5*PI/180;
dir = polar(abs(dir), arg(dir) + angle);
if(abs(dir.real())<A)
{
dir.real()=0;
}
if(abs(dir.imag())<A)
{
dir.imag()=0;
}
directionX=dir.real();
directionY=dir.imag();
}
void TurnRight()
{
const double angle = 5*PI/180;
dir = polar(abs(dir), arg(dir) - angle);
if(abs(dir.real())<A)
{
dir.real()=0;
}
if(abs(dir.imag())<A)
{
dir.imag()=0;
}
directionX=dir.real();
directionY=dir.imag();
}
};
这是我项目的删减,我不敢在这里发布 456 行代码。
我有一个 C++ 项目要提交,我在我的电脑上准备了它。我不得不使用复杂的模板,而且效果很好。但是当我想在另一台电脑上运行我的代码时,它在 complex
类的 real()
和 imag()
函数中显示错误。我不知道为什么它根本没有编译。
最佳答案
查看 complex
的文档模板成员函数。您不能更改行中实部的值:
dir.real()=0;
取而代之的是,您应该将新值作为参数传递:
dir.real(0);
在 dir.imag()=0;
有同样的问题
顺便说一句,您的代码片段无法编译,因为未声明directionX
、directionY
和其他几个变量。
关于c++ - 为什么复杂的模板不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39821335/