不要太过于谨慎,我敢说你 . 我已经在'namespace'之前使用了预期的嵌套名称说明符查看了所有主题,'string没有命名类型,期望'}'ar输入结束而且没有一个工作/帮助 .
#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;
}
现在,我不必包括类的所有.h和.cpp文件,因为它们工作得很好,当我试图添加这个函数时问题就出现了 . 我不知道为什么这不起作用 . 保存是一种虚拟方法,它取决于类计算圆和三角形的数据,并将数据保存为字符串到.txt文件 .
使用namespace std在'namespace'之前预期的nested-name-specifier; ^'string'没有命名类型void funkcja(Shape ** tab,int size,const string&path)^ expect'}'在输入结尾} ^'string'未在此范围字符串name = path中声明; ^'name'未在此范围名称中声明= i; ^'cout'未在此范围内声明cout <<“Hello World!”<endl未声明,cout未声明等 .
#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;
}