关于c++ primer书和其中的程序问题 谢谢
最近在学c和c++,学c时候一直在用vc6.0,感觉也很好。这两天刚接触c++,看的是《c++primer》这本书,但是首先是按照书上的程序打到vc6.0中始终编译时会有...
最近在学c和c++,学c时候一直在用vc 6.0,感觉也很好。这两天刚接触c++,看的是《c++ primer》这本书,但是首先是按照书上的程序打到vc 6.0中始终编译时会有错误,在网上查说是vc 6.0本身就比较老,对c++支持不好,后来下了vs 2010,可是在编译时得到的错误信息是一样的,最后下了dev c++,同样编译不成功(程序输的没问题,输了很多遍)。想问是这本书比较老还是什么其他的问题。
这是其中我编译一直出错的程序,(是按书中输入的):
#include <iostream>
#include <string>
int main()
{
string word;
while ( cin >> word )
cout >> "word read is: " >> word >> '\n';
cout >> "ok: no more words to read: bye!\n";
return 0;
}
vc中错误信息:
e:\visual c++\msdev98\bin\xssss.cpp(5) : error C2065: 'string' : undeclared identifier
e:\visual c++\msdev98\bin\xssss.cpp(5) : error C2146: syntax error : missing ';' before identifier 'word'
e:\visual c++\msdev98\bin\xssss.cpp(5) : error C2065: 'word' : undeclared identifier
e:\visual c++\msdev98\bin\xssss.cpp(6) : error C2065: 'cin' : undeclared identifier
e:\visual c++\msdev98\bin\xssss.cpp(7) : error C2065: 'cout' : undeclared identifier
e:\visual c++\msdev98\bin\xssss.cpp(7) : error C2297: '>>' : illegal, right operand has type 'char [15]'
e:\visual c++\msdev98\bin\xssss.cpp(8) : error C2297: '>>' : illegal, right operand has type 'char [33]'
devc++中的错误信息:
C:\Users\Administrator\Desktop\未命名2.cpp In function `int main()':
5 C:\Users\Administrator\Desktop\未命名2.cpp `string' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
5 C:\Users\Administrator\Desktop\未命名2.cpp expected `;' before "word"
6 C:\Users\Administrator\Desktop\未命名2.cpp `cin' undeclared (first use this function)
6 C:\Users\Administrator\Desktop\未命名2.cpp `word' undeclared (first use this function)
7 C:\Users\Administrator\Desktop\未命名2.cpp `cout' undeclared (first use this function) 展开
这是其中我编译一直出错的程序,(是按书中输入的):
#include <iostream>
#include <string>
int main()
{
string word;
while ( cin >> word )
cout >> "word read is: " >> word >> '\n';
cout >> "ok: no more words to read: bye!\n";
return 0;
}
vc中错误信息:
e:\visual c++\msdev98\bin\xssss.cpp(5) : error C2065: 'string' : undeclared identifier
e:\visual c++\msdev98\bin\xssss.cpp(5) : error C2146: syntax error : missing ';' before identifier 'word'
e:\visual c++\msdev98\bin\xssss.cpp(5) : error C2065: 'word' : undeclared identifier
e:\visual c++\msdev98\bin\xssss.cpp(6) : error C2065: 'cin' : undeclared identifier
e:\visual c++\msdev98\bin\xssss.cpp(7) : error C2065: 'cout' : undeclared identifier
e:\visual c++\msdev98\bin\xssss.cpp(7) : error C2297: '>>' : illegal, right operand has type 'char [15]'
e:\visual c++\msdev98\bin\xssss.cpp(8) : error C2297: '>>' : illegal, right operand has type 'char [33]'
devc++中的错误信息:
C:\Users\Administrator\Desktop\未命名2.cpp In function `int main()':
5 C:\Users\Administrator\Desktop\未命名2.cpp `string' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
5 C:\Users\Administrator\Desktop\未命名2.cpp expected `;' before "word"
6 C:\Users\Administrator\Desktop\未命名2.cpp `cin' undeclared (first use this function)
6 C:\Users\Administrator\Desktop\未命名2.cpp `word' undeclared (first use this function)
7 C:\Users\Administrator\Desktop\未命名2.cpp `cout' undeclared (first use this function) 展开
4个回答
2012-06-17
展开全部
少了 using namespace std;
而且 cout 配合 << 操作符。不是 cout >> 这样是错误的。
cout >> "word read is: " >> word >> '\n';
cout >> "ok: no more words to read: bye!\n";
改为:
cout << "word read is: " << word << '\n';
cout << "ok: no more words to read: bye!\n";
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word;
while ( cin >> word )
cout << "word read is: " << word << '\n';
cout << "ok: no more words to read: bye!\n";
return 0;
}
而且 cout 配合 << 操作符。不是 cout >> 这样是错误的。
cout >> "word read is: " >> word >> '\n';
cout >> "ok: no more words to read: bye!\n";
改为:
cout << "word read is: " << word << '\n';
cout << "ok: no more words to read: bye!\n";
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word;
while ( cin >> word )
cout << "word read is: " << word << '\n';
cout << "ok: no more words to read: bye!\n";
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你是c++的程序,需要在
#include <iostream>
#include <string>
后面加上:
using namespace std;
#include <iostream>
#include <string>
后面加上:
using namespace std;
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using namespaces std;
就可以了
就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using namespace std;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询