连微软给出的例子c2552.cpp在vc6.0下都无法通过!各位大哥请教了!

guijiewan 2008-12-07 07:07:33
// C2552.cpp
// compile with: /clr
#include <string>
using namespace std;

struct Pair_Incorrect {
private:
string m_name;
double m_val;
};

struct Pair_Correct1 {
public:
Pair_Correct1(string name, double val)
: m_name(name), m_val(val) {}

private:
string m_name;
double m_val;
};

struct Pair_Correct2 {
public:
string m_name;
double m_val;
};

int main() {
// To fix, add a constructor to this class and use it for
// initializing the data members, see Pair_Correct1 (below)
// or
// Do not have any private or protected non-static data members,
// see Pair_Correct2 (below). Pair_Correct2 is not recommended in
// case your object model requires some non-static data members to
// be private or protected

string name("John");
Pair_Incorrect pair1 = { name, 0.0 }; // C2552

// initialize a CLR immutable value type that has a constructor
System::DateTime dt = {2001, 4, 12, 22, 16, 49, 844}; // C2552

Pair_Correct1 pair2( name, 0.0 );
Pair_Correct1 pair3 = Pair_Correct1( name, 0.0 );
Pair_Correct2 pair4 = { name, 0.0 };
System::DateTime dt2(2001, 4, 12, 22, 16, 49, 844);
}



我关心主要问题是 Pair_Correct2 pair4 = { name, 0.0 };出现c2552错误.既精简为下面程序还是出错:
// C2552.cpp
// compile with: /clr
#include <string>
using namespace std;



struct Pair_Correct2 {
public:
string m_name;
double m_val;
};

int main() {

string name("John");

Pair_Correct2 pair4 = { name, 0.0 };

}
...全文
196 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eleven 2011-04-05
  • 打赏
  • 举报
回复
08年的帖子~
OObigO 2011-04-05
  • 打赏
  • 举报
回复
把string改成 char 数组就行了。
菜牛 2008-12-08
  • 打赏
  • 举报
回复
VC6支持CLR?第一次听说。而且VC 6对STL支持也不好,似乎也不支持<string>这样新的C++头文件。

另外没用VC 6很多年了,无法重现错误。建议还是用新一点的编译器吧。
guijiewan 2008-12-07
  • 打赏
  • 举报
回复
错误消息
“identifier”: 不能用初始值设定项列表初始化非聚合


错误地初始化了聚合标识符。

聚合定义为:

数组

没有以下内容的类、结构和联合:

构造函数

私有或受保护的成员

基类

虚函数

按定义,
struct Pair_Correct2 {
public:
char m_name;
double m_val;
};
应该是一个聚集(aggregate)才对
不应该出现:non-aggregates cannot be initialized with initializer list 非聚合错误


guijiewan 2008-12-07
  • 打赏
  • 举报
回复
安装了sp6还是不行!不懂了......
guijiewan 2008-12-07
  • 打赏
  • 举报
回复
//第一次点击发帖没反应,就多点了一次,不好意思:-)
//把结构体里的string改为其他类型,比如char,初始化时相应改变,又可以了,为什么呢?如下所示:
这个问题在Bjarne Stroustrup的 < <c++程序设计语言>>Page.89/90页里遇到,有哪位朋友运行过那个例子程序吗?
// C2552.cpp
// compile with: /clr
#include <string>
using namespace std;


struct Pair_Correct2 {
public:
char m_name;
double m_val;
};

int main() {

string name("John");

Pair_Correct2 pair4 = { 'a', 0.0 };

}
sys0006 2008-12-07
  • 打赏
  • 举报
回复
VC6对STL支持不够好
guijiewan 2008-12-07
  • 打赏
  • 举报
回复
//第一次点击发帖没反应,就多点了一次,不好意思:-)
//把结构体里的string改为其他类型,比如char,初始化时相应改变,又可以了,为什么呢?如下所示:
这个问题在Bjarne Stroustrup的<<c++程序设计语言>>Page.89/90页里遇到,有哪位朋友运行过那个例子程序吗?
// C2552.cpp
// compile with: /clr
#include <string>
using namespace std;



struct Pair_Correct2 {
public:
char m_name;
double m_val;
};

int main() {

string name("John");

Pair_Correct2 pair4 = { 'a', 0.0 };

}

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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