我编写了一个带有一个基本数据结构的头文件。
ProdList.h
#ifndef LISTOFITEMS_H
#define LISTOFITEMS_H
struct ListOfItems
{
public:
std::string fdcustid;
std::string fdstkid;
std::string fdordisquantity;
std::string fdordsstatus; // <> 'H'
std::string fdordhtype; // <> 'A'
};
#endif /* GRANDFATHER_H */
现在我有一个数据结构,我将它包含在类定义中,并使用“ProdContainer”类中的数据结构“ListOfItems”。
ProdContainer.h
#include "ProdList.h"
class ProdContainer
{
public:
ProdContainer(void);
~ProdContainer(void);
static void SetNumberOfElements(int Elements);
std::vector<ListOfItems> Items;
}
现在我在Main中写下以下内容。
int _tmain(int argc, _TCHAR* argv[])
{
ProdContainer myobject;
myobject.Items.resize(12);
printf("The size of Items is %i \n", myobject.Items.size());
return 0;
}
一切按预期进行,我得到以下输出。
The size of Items is 12
这一切都很好,很好。但是,我想在类中封装数据,只允许通过类函数进行访问。 当我将以下代码添加到“SetNumberOfElements”实现时,会出现问题。
void ProdContainer::SetNumberOfElements(int Elements)
{
Items.resize(Elements);
}
当我尝试编译这个“错误C2228左边的'.resize'必须有class / struct / union”时出现,我不知道下一步该做什么。 我搜索了高低,似乎找不到符合这个特定问题的帖子,这可能是一个小学生的错误。我已经在错误C2228上检查了MSDN站点,据我所知,Items是结构类型ListOfItems的一个经证实的变量,所以我不明白为什么会出现这个错误。 有没有一种方法可以访问结构的向量或其他方面,我只是看不到。 请帮忙,我准备爆炸了。
答案 0 :(得分:0)
您无法从静态函数访问非静态类成员。
您可以做的是从静态“切换”到非静态。
static void SetNumberOfElements( void * lParam, int Elements)
{
((ProdContainer*)lParam)->Items.resize( Elements );
}
在课堂上使用它:
SetNumberOfELements( this, 10 );
答案 1 :(得分:0)
您只能从static
个功能访问static
个数据。所以改变这个
static void SetNumberOfElements(int Elements);
到此
void SetNumberOfElements(int Elements);