问两个巨弱的C++问题

Auto_And_Elec 2002-01-15 03:08:50
1.将数组的引用作为函数的参数,改如何写?比如:
#include <iostream>
int main(int argc, char* argv[])
{
void printM(std::string [] &, int); //函数申明
std::string m[12];
for (int i = 0; i < 12; i++)
m[i] = "asdjfaldsf";
printM(&m, 12); //调用函数
return 0;
}

void printM(std::string & sp[12], int i) //函数定义
{
.........................
}

请问在这个函数的申明,定义以及调用中,这个数组的引用作为参数该如何写?



2。#include <iostream>
std::string sWord;
cin >> sWord; //这里有错
error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)
Error executing cl.exe.
为何有错?




谢谢1
...全文
61 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ustc 2002-01-15
  • 打赏
  • 举报
回复

完整的:
#include <iostream>
#include <string>

void printM(std::string sp[], int num) ; //声明

int main(int argc, char* argv[])
{
std::string m[12];
for (int i = 0; i < 12; i++)
m[i] = "asdjfaldsf";
printM(m, 12);
return 0;
}

void printM(std::string sp[], int num) //函数定义
{
for(long i=0;i<num;i++)
std::cout<< sp[i];
}




ustc 2002-01-15
  • 打赏
  • 举报
回复

我来晚了!
呵呵。

调用 应该是:
std::string m[12];
for (int i = 0; i < 12; i++)
m[i] = "asdjfaldsf";
printM(m, 12);
tpProgramer 2002-01-15
  • 打赏
  • 举报
回复

第一个问题:
数组本身就是引用! 所以照下面的方法写:

void printM(std::string sp[], int num) //函数定义
{
for(long i=0;i<num;i++)
std::cout<< sp[i];
}


tpProgramer 2002-01-15
  • 打赏
  • 举报
回复

现在你要使用 string , 必须先包含这个库 string
在文件前面中加入 :
#include <string>

tpProgramer 2002-01-15
  • 打赏
  • 举报
回复
先说第二个问题:
给你代码,你看看你的问题就知道了!

#include <iostream>

void main()
{
std::cout << "Hello ";
using namespace std;
cout << "World." << endl;
}

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧