| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2525 人关注过本帖
标题:求助关于string的问题
只看楼主 加入收藏
windyfzz
Rank: 2
等 级:禁止访问
帖 子:90
专家分:12
注 册:2010-7-25
结帖率:68%
收藏
 问题点数:0 回复次数:28 
求助关于string的问题
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
struct Monster
{
    char* name;   //为什么这行改为 string name;程序就通不过呢。如果非得用string name;后面的应该怎么改呢
    int power;
    int agile;

}mon;

void monster()
{
    Monster mon[3]={"野猴",45,50,"山猪",43,43,"老虎",32,49};
    int i;
    for(i=0;i<3;i++)
    {
        cout<<mon[i].name<<mon[i].power<<mon[i].agile<<endl;
    }
}
int main()
{
    monster();
    system("PAUSE");
    return 0;
}
搜索更多相关主题的帖子: string 
2010-07-30 09:55
xxlovemf
Rank: 2
等 级:论坛游民
威 望:1
帖 子:49
专家分:88
注 册:2009-8-6
收藏
得分:0 
j[num] 和 num[j]在编译器看来都是一样的
寻址方式都是基址变址寻址,木有区别
2010-07-30 10:42
mxs810
Rank: 9Rank: 9Rank: 9
来 自:火星
等 级:贵宾
威 望:16
帖 子:234
专家分:1122
注 册:2006-10-19
收藏
得分:0 
以下是引用windyfzz在2010-7-30 09:55:24的发言:

struct Monster
{
    char* name;   //为什么这行改为 string name;程序就通不过呢。如果非得用string name;后面的应该怎么改呢
    int power;
    int agile;

}mon;
不知道你说的通不过是什么意思呢?
std::string name 试试

授人以鱼不如授人以渔
2010-07-30 11:19
gq198718
Rank: 4
等 级:业余侠客
威 望:1
帖 子:86
专家分:200
注 册:2010-6-6
收藏
得分:0 
可以通过吧
2010-07-30 13:10
gq198718
Rank: 4
等 级:业余侠客
威 望:1
帖 子:86
专家分:200
注 册:2010-6-6
收藏
得分:0 
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
struct Monster
{
    //char* name;  
    //为什么这行改为 string name;程序就通不过呢。如果非得用string name;后面的应该怎么改呢
    string name;
    int power;
    int agile;

}mon;

void monster()
{
    Monster mon[3]={"野猴",45,50,"山猪",43,43,"老虎",32,49};
    int i;
    for(i=0;i<3;i++)
    {
        cout<<mon[i].name<<mon[i].power<<mon[i].agile<<endl;
    }
}
int main()
{
    monster();
    system("PAUSE");
    return 0;
}
完全可以通过
2010-07-30 13:13
xiaobaidang
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-7-30
收藏
得分:0 
在c++中建议你不要使用结构体,最好使用类,类是c++的灵魂所在。可以定义构造函数的方式初始化。程序如下
#include <iostream>
#include <string>
using namespace std;

class Monster
{
public:
    Monster(string ,int ,int );

private:
    string name;
    int  power;
    int  agile;
};

Monster::Monster(string nam, int p,int a)
{
    name=nam;
    power=p;
    agile=a;
    cout<<name<<"   "<<power<<"  "<<agile<<endl;
}



int main()
{
    Monster mon[3]={Monster("monkey",45,50),Monster("tiger",43,43),Monster("mouse",32,49)};
    return 0;
}
收到的鲜花
  • mxs8102010-07-30 18:08 送鲜花  5朵   附言:写的不错,很流畅、很工整
2010-07-30 15:02
windyfzz
Rank: 2
等 级:禁止访问
帖 子:90
专家分:12
注 册:2010-7-25
收藏
得分:0 
回复 5楼 gq198718
不会把,为什么我的VC就通不过呢。错误如下
error C2440: 'initializing' : cannot convert from 'char [5]' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : error C2440: 'initializing' : cannot convert from 'const int' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : error C2440: 'initializing' : cannot convert from 'const int' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : error C2440: 'initializing' : cannot convert from 'char [5]' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : error C2440: 'initializing' : cannot convert from 'const int' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : error C2440: 'initializing' : cannot convert from 'const int' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : error C2440: 'initializing' : cannot convert from 'char [5]' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : error C2440: 'initializing' : cannot convert from 'const int' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : error C2440: 'initializing' : cannot convert from 'const int' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : fatal error C1903: unable to recover from previous error(s); stopping compilation
执行 cl.exe 时出错.
2010-07-30 15:22
windyfzz
Rank: 2
等 级:禁止访问
帖 子:90
专家分:12
注 册:2010-7-25
收藏
得分:0 
回复 3楼 mxs810
就是编译时会报错,错误如下
error C2440: 'initializing' : cannot convert from 'char [5]' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : error C2440: 'initializing' : cannot convert from 'const int' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : error C2440: 'initializing' : cannot convert from 'const int' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : error C2440: 'initializing' : cannot convert from 'char [5]' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : error C2440: 'initializing' : cannot convert from 'const int' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : error C2440: 'initializing' : cannot convert from 'const int' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : error C2440: 'initializing' : cannot convert from 'char [5]' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : error C2440: 'initializing' : cannot convert from 'const int' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : error C2440: 'initializing' : cannot convert from 'const int' to 'struct Monster'
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\documents and settings\owner.legend-jh4xyv6s\桌面\cpp1.cpp(17) : fatal error C1903: unable to recover from previous error(s); stopping compilation
执行 cl.exe 时出错.
2010-07-30 15:23
gq198718
Rank: 4
等 级:业余侠客
威 望:1
帖 子:86
专家分:200
注 册:2010-6-6
收藏
得分:0 
我在VS2008里面调试的 程序没问题呢
图片附件: 游客没有浏览图片的权限,请 登录注册
2010-07-30 15:29
gq198718
Rank: 4
等 级:业余侠客
威 望:1
帖 子:86
专家分:200
注 册:2010-6-6
收藏
得分:0 
那是结果
2010-07-30 15:29
快速回复:求助关于string的问题
数据加载中...
 
   



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

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