C++ 不知从何而来的各种错误,f.e.';字符串';即使使用<;也不命名类型;字符串>;及
我敢说,不要太轻率地否决。我已经在这里查看了所有主题,在“namespace”之前使用了预期的嵌套名称说明符,“string不命名类型,预期输入结尾为“}”,但没有一个有效/有帮助C++ 不知从何而来的各种错误,f.e.';字符串';即使使用<;也不命名类型;字符串>;及,c++,virtual,C++,Virtual,我敢说,不要太轻率地否决。我已经在这里查看了所有主题,在“namespace”之前使用了预期的嵌套名称说明符,“string不命名类型,预期输入结尾为“}”,但没有一个有效/有帮助 #include <iostream> #include <string> #include "shape.h" #include "circle.h" #include "triangle.h" using namespace std; void function (Shape **ta
#include <iostream>
#include <string>
#include "shape.h"
#include "circle.h"
#include "triangle.h"
using namespace std;
void function (Shape **tab, int size, const string & path)
{
for(int i=0;i<size;i++)
{
string name = path;
name+=i;
tab[i]->Save(name);
}
}
int main()
{
cout<<"Hello World!"<<endl;
return 0;
}
#包括
#包括
#包括“shape.h”
#包括“circle.h”
#包括“triangle.h”
使用名称空间std;
void函数(形状**选项卡、整数大小、常量字符串和路径)
{
for(int i=0;iSave(name);
}
}
int main()
{
您的一个include文件中可能缺少分号。再次,我们需要一个。否则我们只能猜测。这可能是您的三个标题之一造成的。现在,使用editcircle.h
缺少}
before#endif//CIRCLE_H
@3保存在提供代码的编辑完成之前,问题已关闭。关闭原因在当时是有效的,但随着代码的添加,它现在转移到一个印刷错误的问题上;重新打开问题只是为了一个非常类似的原因而关闭它是没有意义的(离题)原因。
#ifndef SHAPE_H
#define SHAPE_H
#include <string>
#include <fstream>
class Shape
{
public:
struct Point
{
float x, y;
};
virtual bool Save(const std::string &) = 0;
};
#endif // SHAPE_H
#ifndef TRIANGLE_H
#define TRIANGLE_H
#include "shape.h"
class Triangle : public Shape
{
Point a, b, c;
public:
Triangle(Point a, Point b, Point c) : a(a),b(b),c(c) {}
bool Save(const std::string & sciezka);
};
#endif // TRIANGLE_H
#include "triangle.h"
#include <fstream>
#include <cmath>
using namespace std;
bool Triangle::Save(const std::string & sciezka)
{
fstream plik;
plik.open(sciezka,ios::out);
if(plik.good())
{
float ab, bc, ac;
ab = sqrt(pow(b.x-a.x,2)+(pow(b.y-b.x,2)));
bc = sqrt(pow(c.x-b.x,2)+(pow(c.y-b.x,2)));
ac = sqrt(pow(c.x-a.x,2)+(pow(c.y-a.x,2)));
float obw = ab+bc+ac;
float p = (obw)*0.5;
float P = sqrt(p*(p-ab)*(p-bc)*(p-ac));
plik<<"Triangle"<<endl; //Napis
plik<<a.x<<" "<<a.y<<endl; //Współrzędne punktów
plik<<b.x<<" "<<b.y<<endl;
plik<<c.x<<" "<<c.y<<endl;
plik<<obw<<endl; //obwód
plik<<P<<endl; //Pole
plik.close();
return true;
}
else return false;
}
#ifndef CIRCLE_H
#define CIRCLE_H
#include "shape.h"
class Circle : public Shape
{
Point a;
float r;
public:
Circle(Point a, const float r) : a(a),r(r) {}
bool Save(const std::string & sciezka);
#endif // CIRCLE_H
#include "circle.h"
#include <fstream>
#include <cmath>
using namespace std;
bool Circle::Save(const std::string &sciezka)
{
fstream plik;
plik.open(sciezka, ios::out);
if(plik.good())
{
float obwod = 2*M_PI*r;
float P = M_PI*r*r;
plik<<"Circle"<<endl;
plik<<a.x<<" "<<a.y<<endl;
plik<<r<<endl;
plik<<obwod<<endl;
plik<<P<<endl;
return true;
}
else return false;
}