少女时代小分队饭拍:一个简单C++例子?

来源:百度文库 编辑:高考问答 时间:2024/04/27 23:54:54
这是程序代码:
#include <iostream.h>
#include <string.h>
#include "stdio.h"
void main()
{
string name="Niels Stroustrup";
string s=name.substr(6,10);
name.replace(0,5,"Nicholas");
printf("name:%s\n",name.c_str());
}
|
|
|
|这是程序出错提示:
--------------------Configuration: A_TEST - Win32 Debug--------------------
Compiling...
temp.cpp
E:\MATE\VC\A_TEST\temp.cpp(6) : error C2065: 'string' : undeclared identifier
E:\MATE\VC\A_TEST\temp.cpp(6) : error C2146: syntax error : missing ';' before identifier 'name'
E:\MATE\VC\A_TEST\temp.cpp(6) : error C2065: 'name' : undeclared identifier
E:\MATE\VC\A_TEST\temp.cpp(6) : error C2440: '=' : cannot convert from 'char [17]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
E:\MATE\VC\A_TEST\temp.cpp(7) : error C2146: syntax error : missing ';' before identifier 's'
E:\MATE\VC\A_TEST\temp.cpp(7) : error C2065: 's' : undeclared identifier
E:\MATE\VC\A_TEST\temp.cpp(7) : error C2228: left of '.substr' must have class/struct/union type
E:\MATE\VC\A_TEST\temp.cpp(8) : error C2228: left of '.replace' must have class/struct/union type
E:\MATE\VC\A_TEST\temp.cpp(9) : error C2228: left of '.c_str' must have class/struct/union type
执行 cl.exe 时出错.

temp.obj - 1 error(s), 0 warning(s)

哪位高手经编译成功后,给我代码,谢谢!!
<
<
<
_____using namespace std;
_____我加了,不行啊,你能经编译成功后,把完整的程序给我吗?
>
>
>

#include <iostream>
#include <sstream>
#include <string>
#include <cstdio>
using namespace std;
int main()
{
string name="Niels Stroustrup";
string s=name.substr(6,10);
name.replace(0,5,"Nicholas");
printf("name:%s\n",name.c_str());
}
试试

在所有头文件后面加上一句
using namespace std;

name是什么数据类型,是个类么?,还有那种用法吗,能给它直接那样赋值?我怎么不知道,

呵呵C++......呵呵string型......你真厉害......

能把stdio.h发来吗?