【问题标题】:C++ keeping a list of objects and calling a constructor through another functionC ++保留对象列表并通过另一个函数调用构造函数
【发布时间】:2010-11-25 11:02:36
【问题描述】:

为什么没有创建我的对象?
我怎样才能从我的构造函数中执行AllReferrals.push_back(this);?

当我这样做时,我被告知

错误 C2065:“AllReferrals”:未声明的标识符

还有

错误 C2228:'.push_back' 左侧必须有类/结构/联合

如果我把列表初始化放在我得到的类之前

错误 C2065:“AllReferrals”:未声明的标识符

这是我的代码:

class Referral
{
public:
    string url;
    map<string, int> keywords;

    static bool submit(string url, string keyword, int occurrences)
    {
        //if(lots of things i'll later add){
        Referral(url, keyword, occurrences);
        return true;
        //}
        //else
        //    return false;
    }

private:
    list<string> urls;

    Referral(string url, string keyword, int occurrences)
    {
        url = url;
        keywords[keyword] = occurrences;
        AllReferrals.push_back(this);
    }
};

static list<Referral> AllReferrals;

int main()
{
    Referral::submit("url", "keyword", 1);
}

【问题讨论】:

  • 我不是你的编译器。你为什么不解释你想问什么?
  • 抱歉,只是想让事情变得有趣。
  • @Nona:我仍然有我在上一个问题中给你的关于相同代码的相同建议(它的以前版本):你应该说明你的意图是什么,然后人们可以帮助你使用 cmets关于如何实现你的目标。

标签: c++ list object constructor private


【解决方案1】:

从上到下阅读文件时,必须在使用之前声明 AllReferrals 名称。

如果没有声明,编译器不知道它的存在。这就是错误所说的。

要解决您的问题,只需在使用声明之前移动声明,并为您需要名称但尚未定义的类型使用“前向声明”。

#include <iostream>
#include <fstream>
#include <regex>
#include <string>
#include <list>
#include <map>

using namespace std;
using namespace tr1;

class Referral; // forward declaration
list<Referral> AllReferrals; // here 'static' is not needed

class Referral
{
public:
 string url;
 map<string, int> keywords;

 static bool submit(string url, string keyword, int occurrences)
 {
  //if(lots of things i'll later add){
   Referral(url, keyword, occurrences);
   return true;
  //}
  //else
  // return false;
 }

private:
 list<string> urls;

 Referral(string url, string keyword, int occurrences)
 {
  url = url;
  keywords[keyword] = occurrences;
  AllReferrals.push_back(this);
 }
};


int main()
{
 Referral::submit("url", "keyword", 1);
 cout << AllReferrals.size();
 cout << "\n why does that ^^ say 0  (help me make it say one)?";
 cout << "\n and how can i AllReferrals.push_back(this) from my constructor?";
 cout << " When I do it like so, I am told  error C2065: 'AllReferrals' : undeclared identifier";
 cout << " as well as error C2228: left of '.push_back' must have class/struct/union.";
 cout << " If I put the list initialization before the class I get error C2065: 'AllReferrals' : undeclared identifier.";
 cout << "\n\n\t Thanks!";
 getchar();
}

正如评论者所添加的,另一个解决方案是将构造函数定义移动到 AllReferrals 定义下。 无论如何,这始终是一个名称幻影顺序问题。

【讨论】:

  • +1: ... 将构造函数的实现移到列表定义的下方。
  • 也应该是AllReferrals.push_back(*this);
  • 我还需要取消引用我的对象,所以 push_back(*this);
  • 哈哈,没看到ping,谢谢!但我自己想通了:)
  • pinggw33n>是的,刚刚想到,将添加。 dribeas> 是的,你是对的,也添加了。
【解决方案2】:

试试这样的:

...

class Referral;

static list<Referral> AllReferrals;

class Referral
{
...
};

【讨论】:

    【解决方案3】:

    您的第一个问题是必须先声明 AllReferrals 才能使用它。但是,必须在声明 AllReferrals 之前定义 Referral 类。您可以通过拆分 Referral 构造函数的声明和定义来解决此问题,如下所示:

    .
    .
    .
        Referral(string url, string keyword, int occurrences); // declaration
    };
    
    static list<Referral> AllReferrals;
    
    Referral::Referral(string url, string keyword, int occurrences) // definition
    {
        url = url;
        keywords[keyword] = occurrences;
        AllReferrals.push_back(this);
    }
    .
    .
    .
    

    那么你需要意识到this 是一个指针,并且你已经声明了你的列表来存储实际的对象数据。您可以通过两种方式解决此问题:

    • 将AllReferrals.push_back(this); 更改为AllReferrals.push_back(*this);。这将导致将对象的副本放在列表中,这将正常工作,但可能会影响性能。

    • 将static list&lt;Referral&gt; AllReferrals; 的声明更改为static list&lt;Referral *&gt; AllReferrals;。这本身不会为您提供工作代码,因为Referral 是在submit() 的本地创建的,当您离开submit() 时,该对象将被丢弃并且指针悬空。您可以将submit() 中的Referral(url, keyword, occurrences); 更改为new Referral(url, keyword, occurrences);,这将导致对象持续到deleted;但是你必须确保你delete所有Referral对象在你完成它们后以这种方式创建,否则你会泄漏内存——不是永久的,当你的应用程序终止时,操作系统会在你之后清理,但是如果您打算创建大量它们,它可能会给您带来问题。

    【讨论】:

    • +1,当人们问但不阅读答案时,我很难过......您刚刚发布的完全相同的更正是我在上一个问题中发布的。也许如果有足够多的人坚持...
    • 对不起,我在那种情况下不明白。在这里它帮助了我。为什么会有问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 2014-08-01
    相关资源
    最近更新 更多