簡體   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));

}

您的input()函數的name參數是指向const char的指針。 const表示您無法修改它,因此,如果需要修改它,則不必為const。

也就是說,要真正修復它,請在當前使用char[]char*的任何地方使用std::string並考慮返回對象而不是使用out-parameters。 這將使您的代碼不那么容易出錯,並且更易於理解和理解。

符號“ >>”是運算符。 String類的編寫者包括此運算符,以便僅采用基本類型,當然也采用String類類型。

您有兩種選擇:

  1. 將char數組轉換為字符串
  2. 重載'>>'運算符以獲取char數組並根據需要輸出

如果您真的想找樂子,請查找重載運算符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM