Can I use the ANSI String Class in a normal class
-
Hi, I'm wondering if I can use the ANSI String Class in normal classes. I tried yesterday and got an error.
Product.h
#include
#include
using namespace std;
class Product
{
public:
Product(Product&);
Product(string name, int amount , float price);
float GetPrice();
void SetPrice(float price);
void SetAmount(int Amount);
int GetAmount();
void SetName(string name);
string GetName();
Product();
virtual ~Product();
private:
float itsPrice;
int itsAmount;
string itsName;
};Product.cpp
Product::Product()
{}
Product::~Product()
{}
string Product::GetName()
{
return itsName;
}void Product::SetName(string name)
{
itsName = name;
}int Product::GetAmount()
{
return itsAmount;
}void Product::SetAmount(int Amount)
{
itsAmount = Amount;
}void Product::SetPrice(float price)
{
itsPrice = price;
}float Product::GetPrice()
{
return itsPrice;
}Product::Product(string name, int amount, float price)
{
itsName = name;
itsAmount = amount;
itsPrice = price;
}Product::Product(Product &rhs)
{
itsName = rhs.GetName();
itsPrice = rhs.GetPrice();
itsAmount = rhs.GetAmount();
}main.cpp
#include "iostream.h"
#include "Product.h"
using namespace std;int main()
{
Product p1("Apple",0.25,50);cout << p1.GetName();
int s;
std::cin >> s;return 0;
}The error is : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string,class std::allocator >' (or there is no acceptable conversion) Please help. Thanks in advance Tom -- modified at 8:04 Saturday 4th March, 2006
-
Hi, I'm wondering if I can use the ANSI String Class in normal classes. I tried yesterday and got an error.
Product.h
#include
#include
using namespace std;
class Product
{
public:
Product(Product&);
Product(string name, int amount , float price);
float GetPrice();
void SetPrice(float price);
void SetAmount(int Amount);
int GetAmount();
void SetName(string name);
string GetName();
Product();
virtual ~Product();
private:
float itsPrice;
int itsAmount;
string itsName;
};Product.cpp
Product::Product()
{}
Product::~Product()
{}
string Product::GetName()
{
return itsName;
}void Product::SetName(string name)
{
itsName = name;
}int Product::GetAmount()
{
return itsAmount;
}void Product::SetAmount(int Amount)
{
itsAmount = Amount;
}void Product::SetPrice(float price)
{
itsPrice = price;
}float Product::GetPrice()
{
return itsPrice;
}Product::Product(string name, int amount, float price)
{
itsName = name;
itsAmount = amount;
itsPrice = price;
}Product::Product(Product &rhs)
{
itsName = rhs.GetName();
itsPrice = rhs.GetPrice();
itsAmount = rhs.GetAmount();
}main.cpp
#include "iostream.h"
#include "Product.h"
using namespace std;int main()
{
Product p1("Apple",0.25,50);cout << p1.GetName();
int s;
std::cin >> s;return 0;
}The error is : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string,class std::allocator >' (or there is no acceptable conversion) Please help. Thanks in advance Tom -- modified at 8:04 Saturday 4th March, 2006
Hey Tom: Please repost your question, but this time place your code inside <pre></pre> tags, and click the "Ignore HTML tags in this message" checkbox. It's tough to read your code otherwise. Good luck.
Software Zen:
delete this;