回 帖 发 新 帖 刷新版面

主题:一个简单C++代码,哪里错了?

下面的代码在VS2008里面顺利通过,为什么在VC++6.0里面就编译出错呢?跪求解释。
出现提示:
--------------------Configuration: VC6 - Win32 Debug--------------------
Compiling...
VC6.cpp
G:\编程练习\VC6\VC6.cpp(13) : error C2552: 'stu' : non-aggregates cannot be initialized with initializer list
执行 cl.exe 时出错.

VC6.exe - 1 error(s), 0 warning(s)


#include <iostream>
#include <string>
using namespace std;
struct student 
{
    int num;
    string name;
    char sex;
    int age;
};
int main()
{
    student stu={123,"hhuam",'M',24};
    cout<<"num:\t"<<stu.num<<endl;
    cout<<"name:\t"<<stu.name<<endl;
    cout<<"sex:\t"<<stu.sex<<endl;
    cout<<"age:\t"<<stu.age<<endl;
    return 0;
}

回复列表 (共4个回复)

沙发

VC6的STL库不健全,所以string成员无法用此法初始化。

板凳

哦,知道了,谢谢指点。把 string name;cout<<"name:\t"<<stu.name<<endl;注释掉后果然运行成功了……看来以后这种情况要用字符数组了。

3 楼

VC6使用的STL库最初是为VC4开发的,而该版本的编译器缺乏对模板成员函数的支持——虽然当时几乎所有的C++编译器都没有这样的特性。后来VC6增加了这个特性,可是MS居然忘记了STL.
换sgi吧

4 楼

#include <iostream> 
#include <string> 
#include <map> 
using namespace std; 

class something 

public: 
    void dosomething(const string &item) const 
    { 
        map<string, string>::iterator i = cont.find(item); 
        cout << i -> first << endl << i -> second << endl; 
    } 
private: 
    map<string, string> cont; 
}; 

int main() 

    return 0; 


试试这个在VC6本尊编译器里编译一下,看会出现什么。

我来回复

您尚未登录,请登录后再回复。点此登录或注册