帮忙看一下错误。谢谢。

cycchina 2009-05-30 12:39:52

头文件定义:

class WordTable
{
public:
typedef std::vector<std::string> WordList; ///< A list of words (strings)
typedef std::vector<std::streampos> OccurrenceList; ///< A list of file positions of a word

private:
std::map<std::string, OccurrenceList> table;

public:
/// The constructor generates an empty WordTable
WordTable(void);

/// Add the given WordOccurrence to the table.
void addWord(WordOccurrence word);



同名.cpp文件


/// Add the given WordOccurrence to the table.
void WordTable::addWord(WordOccurrence word)
{
WordList::push_back(word.word);//这里提示错误

//OccurrenceList::push_back(word.position);

table.insert( pair<std::string, OccurrenceList>(WordList, OccurrenceList );

map<std::string, OccurrenceList>::iterator iter;
for (iter = table.begin(); iter != table.end(); iter++)
{
cout << "map.first:"<< iter->first<<endl;
cout << "map.second:"<<iter->second.<< endl;
}
}


错误提示为
visual studio 2008\projects\asst2\wordtable.cpp(18) : error C2352: 'std::vector<_Ty>::push_back' : illegal call of non-static member function

请问,我应该如何该。。 为什么会错误呢?
...全文
74 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
cycchina 2009-05-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 pengzhixi 的回复:]
WordList::push_back(word.word);//你这里没有构造一个容器对象啊.
[/Quote]
你的意识是不是 WOrdList mylist;
mylist.push_back(word.word);
是这个意思么???
pengzhixi 2009-05-30
  • 打赏
  • 举报
回复
WordList::push_back(word.word);//你这里没有构造一个容器对象啊.
red_berries 2009-05-30
  • 打赏
  • 举报
回复
WordList::push_back(word.word);//这里提示错误
WordList只是一个类的名称,要调用成员函数要用对象或指针来调用,只有静态函数才可以这样调用
MMMoonriver 2009-05-30
  • 打赏
  • 举报
回复
up
fairchild811 2009-05-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 red_berries 的回复:]
WordList::push_back(word.word);//这里提示错误
WordList只是一个类的名称,要调用成员函数要用对象或指针来调用,只有静态函数才可以这样调用
[/Quote]

恩,只有静态才能这样
aaaa3105563 2009-05-30
  • 打赏
  • 举报
回复
帮顶·
Sou2012 2009-05-30
  • 打赏
  • 举报
回复
up!!!
red_berries 2009-05-30
  • 打赏
  • 举报
回复
成员函数 必须通过对象或对象指针来调用
red_berries 2009-05-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cycchina 的回复:]
引用 2 楼 pengzhixi 的回复:
WordList::push_back(word.word);//你这里没有构造一个容器对象啊.

你的意识是不是 WOrdList mylist;
mylist.push_back(word.word);
是这个意思么???
[/Quote]
是的

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧