2011-12-13 4 views
4

현재 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

답변

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

Ahh; 알았어! 객체의 인스턴스를 만드는 잘못된 아이디어가 있었기 때문에 좋은 점도 있습니다. 나는 그 물건이 포함 되었기 때문에 그것을 창조했다고 믿었다. 그것은 매력처럼 작용했습니다. 시간 내 주셔서 감사합니다. –

관련 문제