| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 802 人关注过本帖
标题:程序不能运行,怎么回事?
只看楼主 加入收藏
醒山
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:9
帖 子:463
专家分:2071
注 册:2015-5-25
结帖率:88.89%
收藏
已结贴  问题点数:100 回复次数:17 
程序不能运行,怎么回事?
#include<iostream>
#include<string>
struct free_throws
{
    std::string name;
    int made;
    int attempts;
    float percent;
};
void display(const free_throws & ft);
void set_pc(free_throws & ft);
free_throws & accumulate(free_throws & target, const free_throws & source);
int main()
{
    free_throws one = {"Ifelsa Branch",13,14};
    free_throws two = {"Andor Knott",10,16};
    free_throws three ={"Minnie Max ",7,9};
    free_throws four ={"Whily Looper",5,9};
    free_throws five ={"Long Long",6,14};
    free_throws team ={"Throwgoods",0,0};
    free_throws dup;
    set_pc(one);
    display(one);
    accumulate(team,one);
    display(team);
    display(accumulate(team,two));
    accumulate(accumulate(team,three),four);
    display(team);
    dup = accumulate(team,five);
    std::cout<<"Displaying team:\n";
    display(team);
    std::cout<<"Displaying dup after assignment:\n";
    display(dup);
    set_pc(four);
    accumulate(dup,five) = four;
    std::cout<<"Displaying dup after ill-advised assignment:\n";
    display(dup);
    return 0;
}
void display(const free_throws & ft)
{
    using std::cout;
    cout<<"Name: "<<ft.name<<'\n';
    cout<< " Made: "<<ft.made<<'\t';
    cout<< "Attempts: "<<ft.attempts<<'\t';
    cout<<"Percent: "<<ft.percent<<'\n';
}
void set_pc(free_throws & ft)
{
    if(ft.attempts != 0)
        ft.percent = 100.0f *float(ft.made)/float(ft.attempts);
    else
        ft.percent = 0;
}
free_throws & accumulate(free_throws & target,const free_throws & source)
{
    target.attempts += source.attempts;
    target.made += source.made;
    set_pc(target);
    return target;
}
错误信息如下
--------------------Configuration: c3 - Win32 Debug--------------------
Compiling...
c3.cpp
G:\Microsoft Visual Studio\MyProjects\c3\c3.cpp(15) : error C2552: 'one' : non-aggregates cannot be initialized with initializer list
G:\Microsoft Visual Studio\MyProjects\c3\c3.cpp(16) : error C2552: 'two' : non-aggregates cannot be initialized with initializer list
G:\Microsoft Visual Studio\MyProjects\c3\c3.cpp(17) : error C2552: 'three' : non-aggregates cannot be initialized with initializer list
G:\Microsoft Visual Studio\MyProjects\c3\c3.cpp(18) : error C2552: 'four' : non-aggregates cannot be initialized with initializer list
G:\Microsoft Visual Studio\MyProjects\c3\c3.cpp(19) : error C2552: 'five' : non-aggregates cannot be initialized with initializer list
G:\Microsoft Visual Studio\MyProjects\c3\c3.cpp(20) : error C2552: 'team' : non-aggregates cannot be initialized with initializer list
执行 cl.exe 时出错.

c3.obj - 1 error(s), 0 warning(s)
2015-10-19 20:04
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
建立结构体变量时把四个参数输全看成不成。试一试先
2015-10-19 20:28
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
不是参数,我是说成员
2015-10-19 20:29
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
你用的是什么编译器,以及编译器的版本号是多少?
你可以换个正确的编译器试试,因为我怀疑你的编译器有问题,竟然说free_throws是non-aggregates的。
2015-10-20 08:57
Spy001
Rank: 9Rank: 9Rank: 9
来 自:山那边
等 级:贵宾
威 望:10
帖 子:138
专家分:1360
注 册:2015-1-23
收藏
得分:0 

千百年之后,谁又还记得谁........
2015-10-20 10:12
人_神
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:44
专家分:103
注 册:2013-8-1
收藏
得分:0 
free_throws one = {"Ifelsa Branch",13,14};
    free_throws two = {"Andor Knott",10,16};
    free_throws three ={"Minnie Max ",7,9};
    free_throws four ={"Whily Looper",5,9};
    free_throws five ={"Long Long",6,14};
    free_throws team ={"Throwgoods",0,0};
出错的可能原因是 :结构体中float percent没有赋初值,
如free_throws team ={"Throwgoods",0,0};可改成free_throws team ={"Throwgoods",0,0,0.0};
2015-10-20 14:58
醒山
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:9
帖 子:463
专家分:2071
注 册:2015-5-25
收藏
得分:0 
我也是这么想的,但试试没有用
2015-10-20 16:18
wmf2014
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:216
帖 子:2039
专家分:11273
注 册:2014-12-6
收藏
得分:20 
好像是string类不能初始化吧

能编个毛线衣吗?
2015-10-20 16:35
hjx1120
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:李掌柜
等 级:贵宾
威 望:41
帖 子:1314
专家分:6927
注 册:2008-1-3
收藏
得分:80 
VS2012 与 G++ 4.8.1 均编译通过,楼主的不会是上古时代的编译器吧
2015-10-20 16:40
醒山
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:9
帖 子:463
专家分:2071
注 册:2015-5-25
收藏
得分:0 
VC++6.0
2015-10-20 20:27
快速回复:程序不能运行,怎么回事?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016011 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved