我不知道如何在這裏實現解決方案。」預期在'void'之前的非限定id'「&」班級中的字符串沒有指定類型「錯誤
在編譯的錯誤是: -
error: expected unqualified-id before 'void'
error: 'string' in class 'UserDirectory' does not name a type
error: 'string' in class 'UserDirectory' does not name a type
===========
**UserDirectory.cpp**
#include "UserDirectory.h"
#include <iostream>
#include <string>
using namespace std;
UserDirectory::UserDirectory(){
cout << "UserDirectory Constructor created\n\n";
}
UserDirectory::void setName(string x){
name = x;
}
UserDirectory::string getName(){
return name_;
}
UserDirectory::string name;
===========
**UserDirectory.h**
#ifndef USERDIRECTORY_H
#define USERDIRECTORY_H
#include <iostream>
#include <string>
class UserDirectory
{
public:
UserDirectory();
void setName(std::string x);
std::string getName();
private:
std::string name_;
};
#endif // USERDIRECTORY_H
========
**main.cpp**
#include <iostream>
#include <string>
#include "UserDirectory.h"
using namespace std;
int main(){
UserDirectory user1;
string inputName;
cout << "Enter your name: ";
cin >> inputName;
user1.setName(inputName);
cout << "\nYou entered " << user1.getName();
return 0;
} // end main
感謝您的回答dasblinkenlight - 範圍是我知道的菜鳥錯誤!我一定不會再這樣做。 我會閱讀我的靜態/動態變量。 – Vault13