Click here to Skip to main content
15,562,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was working in a project and I had problems

says
C++ namespace has no member


What I have tried:

#include <iostream>
#include<fstream>
#include<math.h>

using namespace std;

class Shape
{
private:
	string color;
	string name;

public:

	Shape()
	{
		color = "";
		name = "";
	}

	Shape(string col, string nm)
	{
		color = col;
		name = nm;
	}
	void setColor(string col)
	{
		cout << "Color:" << color << endl;
		color = col;

	}
	void setName(string nm)
	{
		name = nm;
	}
	void print()
	{
		cout << "Color:" << color << endl;
		cout << "Name:" << name << endl;
	}
	void readData(string col, string nm)
	{
		cout << "readdata Shape" << col << endl;
		setColor(col);
		setName(nm);

	}
	void printToFile(ofstream& output)
	{
		output << color << "," << name << ",";
	}
	virtual void readFromFile(ifstream& input) = 0;


};

class Shape2D :public Shape
{
protected:
	double perimeter;
	double area;

public:
	Shape2D()
	{
		perimeter = 1.0;
		area = 1.0;
	}
	Shape2D(string col, string nm) :Shape(col, nm)
	{
		cout << "param constructor" << endl;
		perimeter = 1.0;
		area = 1.0;
	}
	double getPerimeter() { return perimeter; }
	double getArea() { return area; }
	void setArea(double val)
	{
		area = val;

	}
	void setPerimeter(double val)
	{
		perimeter = val;
	}
	void print()
	{
		Shape::print();
		cout << "Perimeter:" << perimeter << endl;
		cout << "Area:" << area << endl;
	}
	void readData(string col, string nm)
	{
		cout << "Read data Shape2D" << endl;
		setColor(col);
		setName(nm);
		return;
	}
	void printToFile(ofstream& output)
	{
		Shape::printToFile(output);
	}
	virtual void computeArea() = 0;
	virtual void computePerimeter() = 0;
	virtual void readFromFile(ifstream& input) = 0;

};

class Square :public Shape2D
{
private:
	double length;
public:

	Square()
	{
		length = 1.0;
	}
	Square(string col, string nm, double lem) :Shape2D(col, nm)
	{
		length = lem;
	}
	void computeArea()
	{
		setArea(length * length);
	}
	void computePerimeter()
	{
		setPerimeter(4 * length);
	}
	void print()
	{
		Shape2D::print();
	}
	void readData(string col, string nm, double lem)
	{
		length = lem;
		Shape2D::readData(col, nm);
		return;
	}
	void printToFile(ofstream& output)
	{
		Shape::printToFile(output);
		output << length << "," << getArea() << "," << getPerimeter() << "\n";
	}
	void readFromFile(ifstream& input)
	{
		string data = "";
		while (std::getline(input, data))
		{
			std::cout << data << endl;
		}
	}
};

class Triangle :public Shape2D
{
private:
	double length;
public:
	Triangle()
	{
		length = 1.0;
	}
	Triangle(string col, string nm, double lem) :Shape2D(col, nm)
	{
		length = lem;
	}
	void computeArea()
	{
		setArea(0.433 * length * length);
	}
	void computePerimeter()
	{
		setPerimeter(3 * length);
	}
	void print()
	{
		Shape2D::print();
	}
	void readData(string col, string nm, double lem)
	{
		cout << "Read data triangle" << endl;
		Shape2D::readData(col, nm);
		length = lem;
		return;
	}
	void printToFile(ofstream& output)
	{
		Shape::printToFile(output);
		output << length << "," << getArea() << "," << getPerimeter() << "\n";
	}
	void readFromFile(ifstream& input)
	{
		string data = "";
		while (std::getline(input, data))
		{
			std::cout << data << endl;
		}
	}
};

class Hexagon :public Shape2D
{
private:
	double length;
public:
	Hexagon()
	{
		length = 1.0;
	}
	Hexagon(string col, string nm, double lem) :Shape2D(col, nm)
	{
		length = lem;
	}
	void computeArea()
	{
		setArea((3 * sqrt(3) * pow(length, 2)) / 2.0);
	}
	void computePerimeter()
	{
		setPerimeter(6 * length);
	}
	void setLength(double len)
	{
		cout << "settingb length set" << endl;
		length = len;
	}
	void print()
	{
		Shape2D::print();
	}
	void readData(string col, string nm, double lem)
	{
		cout << "Read data hexagon:" << col << "\t" << nm << "\t" << lem << endl;
		setLength(lem);
		cout << "length set" << endl;
		setColor(col);
		setName(nm);

		return;
	}
	void printToFile(ofstream& output)
	{
		Shape::printToFile(output);
		output << length << "," << getArea() << "," << getPerimeter() << "\n";
	}
	void readFromFile(ifstream& input)
	{
		string data = "";
		while (std::getline(input, data))
		{
			std::cout << data << endl;
		}
	}
};

int main()
{
	ofstream myfile;
	myfile.open("Shapes.dat");
	Square* obj = new Square("Blue", "Square", 4.0);
	obj->computeArea();
	obj->computePerimeter();
	obj->print();
	obj->printToFile(myfile);



	Triangle* obj2 = new Triangle("Yellow", "Triangle", 12.0);
	obj2->computeArea();
	obj2->computePerimeter();
	obj2->print();
	obj2->printToFile(myfile);

	Hexagon obj3;
	string col, name;
	double length;
	cout << "Enter color,name,length of hexagon" << endl;
	cin >> col;
	cin >> name;
	cin >> length;
	obj3.readData(col, name, length);
	obj3.computeArea();
	obj3.computePerimeter();
	obj3.print();
	obj3.printToFile(myfile);

	myfile.close();

	std::ifstream file;
	file.open("Shapes.dat");
	obj3.readFromFile(file);
	return 0;
}
Posted
Updated 8-May-22 15:22pm

1 solution

Add #include <string>. That fixed the compile error for me.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900