Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 函数在类中声明时应具有原型_C++_Oop - Fatal编程技术网

C++ 函数在类中声明时应具有原型

C++ 函数在类中声明时应具有原型,c++,oop,C++,Oop,我有一个字符串要通过函数打印。我使用的是turbo-c编译器 在使用过程方法时,我可以通过以下代码完成: #include <iostream.h> #include <conio.h> void strr(char name[]); void main(){ char name1[10]; cout << "Enter name"; cin >> name1; strr(name1); getch(); } vo

我有一个字符串要通过函数打印。我使用的是turbo-c编译器

在使用过程方法时,我可以通过以下代码完成:

#include <iostream.h>
#include <conio.h>

void strr(char name[]);
void main(){
   char name1[10];
   cout << "Enter name";
   cin >> name1;
   strr(name1);
   getch();
}

void strr(char name[]){
   cout << name;
}
#包括
#包括
void strr(字符名[]);
void main(){
字符名称1[10];
cout>name1;
strr(名称1);
getch();
}
void strr(字符名[]){
不能命名1;
strr(名称1);
getch();
}
void name::strr(字符名[]){

cout由于函数是在类中定义的,因此需要
名称
类的对象/实例来调用它:

name obj;
cin >> name1;
obj.strr(name1);
或者,如果将函数声明为静态函数,则可以在不使用类实例的情况下调用它,因为该函数是类函数:

类名{
public:static void strr(char name[]){cout name1
名称:strr(名称1);
试试这个 名称::void strr(字符名[])
{}

您正在使用的strr函数不是name类中的函数。顺便说一句,
main
函数总是返回
int
。此外,对于文本数组而不是字符数组,您更喜欢使用
std::string
。数组存在缓冲区溢出问题,传递给函数会很麻烦。
name obj;
cin >> name1;
obj.strr(name1);
class name{
   public: static void strr(char name[]) {cout << name << endl;}
};

...

cin >> name1
name::strr(name1);