简体   繁体   English

我如何克服此错误C2679:二进制'>>':未找到采用'const char []'类型的右侧操作数的运算符

[英]how I can overcome this error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char []'

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <cstring>

void initialize(char[],int*);
void input(const char[] ,int&);
void print ( const char*,const  int);
void growOlder (const char [], int* );

bool comparePeople(const char* ,const int*,const char*,const int*);

int main(){

  char name1[25];
     char name2[25];
     int age1; 
  int age2;


 initialize (name1,&age1);
 initialize (name2,&age2);

 print(name1,age1);
 print(name2,age2);

 input(name1,age1);
 input(name2,age2);

 print(name1,age1);
 print(name2,age2);

 growOlder(name2,&age2);

 if(comparePeople(name1,&age1,name2,&age2))
    cout<<"Both People have the same  name and age "<<endl;
 return 0;
}

void input(const  char name[],int &age)
{
 cout<<"Enter a name :";
 cin>>name ;

 cout<<"Enter an age:";
 cin>>age;
 cout<<endl;
}

void initialize ( char name[],int *age)
{
 name[0]='\0'; 
 *age=0; }
void print ( const char name[],const int age )
{
 cout<<"The Value stored in variable name is :"
   <<name<<endl
  <<"The Value stored in variable  age is :"
   <<age<<endl<<endl;
}

void growOlder(const char name[],int *age)
{
 cout<< name <<" has grown one year older\n\n";
 *age++;
}
bool comparePeople (const char *name1,const int *age1,
     const char *name2,const int *age2)
{

return(*age1==*age2 && !strcmp(name1,name2));

}

The name parameter of your input() function is a pointer to const char . 您的input()函数的name参数是指向const char的指针。 const means you can't modify it, so if you need to modify it, it needs not to be const. const表示您无法修改它,因此,如果需要修改它,则不必为const。

That said, to really fix it, use std::string wherever you currently use char[] s and char* s and consider returning objects instead of using out-parameters; 也就是说,要真正修复它,请在当前使用char[]char*的任何地方使用std::string并考虑返回对象而不是使用out-parameters。 this will make your code much less error prone and easier to follow and understand. 这将使您的代码不那么容易出错,并且更易于理解和理解。

The symbol '>>' is an operator. 符号“ >>”是运算符。 The writer of the String class included this operator to only take primitive types and of course the String class type. String类的编写者包括此运算符,以便仅采用基本类型,当然也采用String类类型。

You have two options: 您有两种选择:

  1. Convert the char array to a string 将char数组转换为字符串
  2. Overload the '>>' operator to take char arrays and output it as you like 重载'>>'运算符以获取char数组并根据需要输出

Look up overloading operator if you really want to have fun. 如果您真的想找乐子,请查找重载运算符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误C2679:二进制&#39;&gt;&gt;&#39;:未找到采用&#39;const char [4]&#39;类型的右侧操作数的运算符(或没有可接受的转换)22 - Error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char [4]' (or there is no acceptable conversion) 22 错误 C2679:二进制“&lt;&lt;”:未找到采用“std::vector”类型的右侧操作数的运算符<char,std::allocator<char> &gt;&#39; - error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::vector<char,std::allocator<char>>' 错误 C2679:二进制“&gt;&gt;:未找到采用“GradeType”类型的右侧操作数的运算符 - error C2679: binary ' >> : no operator found which takes a right-hand operand of type 'GradeType' 错误 C2679 二进制“&lt;&lt;”:未找到采用“T”类型右侧操作数的运算符 - Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'T' 错误:C2679二进制&#39;==&#39;:未找到采用类型为&#39;const std :: string&#39;的右侧操作数的运算符(或者没有可接受的转换 - Error:C2679 binary '==': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion 错误C2679:二进制&#39;==&#39;:未找到采用类型为右侧的操作数的运算符 - error C2679: binary '==' : no operator found which takes a right-hand operand of type 错误C2679:二进制&#39;+&#39;:未找到采用类型为右侧的操作数的运算符 - Error C2679: binary '+' : no operator found which takes a right-hand operand of type 错误 C2679:二进制“[”:未找到采用“const VerseKey”类型的右侧操作数的运算符(或没有可接受的转换) - error C2679: binary '[' : no operator found which takes a right-hand operand of type 'const VerseKey' (or there is no acceptable conversion) 错误C2679:二进制&#39;&lt;&lt;&#39;:找不到运算符,该运算符使用类型为&#39;std :: string&#39;的右侧操作数 - error C2679: binary '<<':no operator found which takes a right-hand operand of type 'std::string' 错误C2679的问题:二进制&#39;+ =&#39;:未找到采用类型为&#39;int&#39;的右侧操作数的运算符 - Issue with error C2679: binary '+=': no operator found which takes a right-hand operand of type 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM