2011-12-13 104 views
4

我目前在教自己C++使用C++ for Dummies All-In-One;第二版。为了创建这个程序,我正在使用Qt。我知道在头文件中组织对象和类,并预期在除main.cpp之外构建的.cpp文件中的成员函数是一个好习惯。在这方面,我尝试在本书中运行这些练习,但最近刚刚遇到了以下错误。错误:预期''之前的主表达式。'代币

expected primary-expression before '.' token 

第31行,32行和37行出现此错误,因此它们看起来与我的类成员函数有关。

我的main.cpp

#include "controlinginput.h" 
#include <QtCore/QCoreApplication> 
#include <iostream> 
#include <sstream> 


using namespace std; 

int main(int argc, char *argv[]) 
{ 
QCoreApplication a(argc, argv); 


// just a basic name-entering 
string name; 
cout << "What is your name?"; 
cin >> name; 
cout << "Hello " << name << endl; 

/* now you are asked for a number 
    but the computer will allow you to enter anything*/ 
int x; 
cout << endl << "Enter a number! Any Number!" << endl; 
cin >> x; 
cout << "You choose " << x << endl; 

/* now youll be asked for a number again 
    but the computer will only allow numbers */ 
cout << endl<< "This time you will ONLY be able to enter a number! " << endl; 
cout << "SO, Pick a number! any number!" << endl; 
string entered = ControlingInput.enterOnlyNumbers(); // ###Error###   
int num = ControlingInput.stringToANumber(entered); // ###Error### 
cout << endl << "You entered " << num << endl; // value is displayed 
//Now finally we enter the password 
cout << endl; 
cout << "Please enter a password" << endl; 
string password = ControlingInput.EnterPassword(); // ###Error### 
cout << "shh... your password is " << password << endl; 
return a.exec(); 
} 

我做了一些研究,发现这个错误表示一个非常广泛的语法的滥用。不幸的是,我无法找到类似于我的具体实例;我希望能从一些更有经验的程序员那里获得一些见解。如果这是一个简单的问题,由于我的疏忽,我提前道歉,并感谢反馈意见。我学得更好,如果它给我配发的,而不是一个小麻烦..

因为这其中就包括我的成员函数我也包括我的头文件和.cpp

controlingInput.cpp(我已经包括了我的头文件和iostreamsstream在这里,但由于某些原因,编辑正在给我放在这里的问题)

using namespace std; 

ControlingInput::ControlingInput() 
{ 

} 
int ControlingInput::stringToANumber(string MyString) 
{ 
istringstream converter(MyString); //Holds the string that was passed to this function 
int result;      //Holds the integer result 

//perform the conversion 
converter >> result; 
return result; //function completes and returns converted string 

} 

string ControlingInput::enterOnlyNumbers() 
{ 
string numbAsString = ""; // this holds our numeric string 
     char ch = getch(); // This gets a single character from our user 
//Says to keep gettting characters from our user untill user presses enter 
     while (ch != '\r') // \r is the enter key 
     { 
      //This says to add characters only if they are numbers 
      if (ch >= '0' && ch <='9') 
      { 
       cout << ch; // show 
       numbAsString += ch; // add character to the string 
      } 

      ch = getch(); // get the next character from the user 

     } 
     return numbAsString; 

} 

string ControlingInput::EnterPassword() 
{ 
string numbAsString = ""; //this will hold our password string 
char ch = getch(); // this gets a single char from our users just like before 
//keep gettting characters from the user until enter/return is pressed 
while (ch != '\r'); // \r is the enter or return key 
{ 
    //for security passwords are displayed as asterisks instead of characters 
    cout << '*'; 

    //add character input into the password string 
    numbAsString += ch; 

    //Get the next character from the user 
    ch = getch(); 
} 
return numbAsString; // return the user input from this function 

,这里是我的controlingInput.h

#ifndef CONTROLINGINPUT_H 
#define CONTROLINGINPUT_H 
#include <iostream> 

using namespace std; 

class ControlingInput 
{ 
public: 
int stringToANumber(string MyString); 
string EnterPassword(); 
string enterOnlyNumbers(); 

}; 

#endif // CONTROLINGINPUT_H 

在此先感谢您的任何反馈意见。

+0

你正在对待你的函数,像`静态`函数,但它们不是'静态'。你必须创建一个`ControlingInput`的实例。 – birryree 2011-12-13 22:02:20

回答

6

您正试图用类本身调用实例变量,就好像它们是静态的(这仍然是无效的语法)。为了正常工作,你需要一个ControlingInput的实例。

int main(int argc, char *argv[]) 
{ 

    QCoreApplication a(argc, argv); 

    ControlingInput ctrlInput; //Create instance 
    ... 

    string entered = ctrlInput.enterOnlyNumbers();   
    int num = ctrlInput.stringToANumber(entered); 
    cout << endl << "You entered " << num << endl; // value is displayed 
    ... 

    string password = ctrlInput.EnterPassword(); 
    cout << "shh... your password is " << password << endl; 
    return a.exec(); 

} 
+0

啊;好吧,我明白了!它也是一件好事,因为我对创建对象的实例有错误的想法。我相信,因为这个对象被包含了,所以它被创建了。这就像一个魅力。感谢您的时间。 – 2011-12-13 22:15:12

相关问题