-1

我有一个info具有多种功能的结构。其中一个函数是info combineInfo并接受两个参数(两组信息)。

Info combineInfo(info1, info2);

main我使用构造函数将所有内容输入到 info1 和 info2 中,一切都很好。但是,combineInfo(info1, info2)main. 我收到消息“错误:combineInfo未在此范围内声明”。

Info info1; //this is from a constructor that inputs all the values from cin.
Info info2; // ^^
Info3 =  combineInfo(info1, info2;
info3.printinfo(); // constructor from class, works fine.

该函数如下所示:

Info Info::combineInfo(Info1, Info2); // sets some values of info1 to info3 and some of info2 to info3. 
4

2 回答 2

1

函数应该是:

  class Info{
    static Info combineInfo(Info info1, Info info2) { 
      //implementation  
    }
    //the rest of the class implementation!
  };

而且由于它是一个成员函数,它需要范围解析操作符::

info3 = Info::combineInfo(info1, info2);这就是你所说的!

您期望的当前实现combineInfo在全局范围内,而它在类(成员函数)内 - 因此出现错误!

于 2013-03-12T06:34:59.000 回答
0

使用信息::combineInfo(info1, info2);

您不能直接访问外部的成员功能。您需要使用范围解析运算符 :: 或 . 访问它。

于 2013-03-12T13:49:38.483 回答