+ -
当前位置:首页 → 问答吧 → 菜鸟请教 (5)

菜鸟请教 (5)

时间:2011-12-04

来源:互联网

大神 帮帮忙把 看看怎么错了 
我是有个Product类,我想在storage类中动态添加Product类的对象,所以用了vector然后就各种错误 ,请大神帮看看,先奉上代码
Product.h
#include <iostream>
using namespace std;
class Product
{
public:
string time1;
string name;
double price;
string kind;
public:
Product(string time,string name,double price,string kind);
string getTime();
string getName();
double getPrice();
string getKind();
};
Product.cpp

#include "Product.h"


Product::Product(string Time1,string Name,double Price,string Kind)
{
time1=Time1;
name=Name;
price=Price;
kind=Kind;
}
string Product::getTime()
{
return time1;
}
string Product::getName()
{
return name;
}
double Product::getPrice()
{
return price;
}
string Product::getKind()
{
return kind;
}
storage.h

#include <iostream>
#include "Product.h"
#include<vector>

using namespace std;
class storage
{
private:
vector<Product> p;


public:
storage(void);
void addProduct(string name,string time,double price,string kind);
};
storage.cpp

#include "storage.h"
#include "Product.h"
storage::storage(void)
{

}
void storage::addProduct(string name,string time,double price,string kind)
{
Product a(time,name,price,kind);
p.push_back(a);
}
菜鸟问题 请耐心解答下 谢谢哈

作者: fireylee   发布时间: 2011-12-04

public:
string time1;
string name;
double price;
string kind;
第一个错误,你这都是公有的,你都直接可以通过点操作来获取数据了,你还设计函数来获取数据,改为私有
#include "storage.h"
#include "Product.h"
storage::storage(void)
{

}
void storage::addProduct(string name,string time,double price,string kind)
{
Product a(time,name,price,kind);
p.push_back(a);
}
这里头文件重复包含了,你应该用
#ifndef ...
#define ...
//头文件。。。
#endif
要用这种格式把头文件处理一下

作者: qscool1987   发布时间: 2011-12-04