错误:应在';之前使用主表达式';代币 我现在用C++来教自己C++,傻瓜都是一体的;第二版。为了创建这个程序,我使用Qt。我认为在头文件中组织对象和类是一种很好的做法,并在除main.cpp之外构建的.cpp文件中组织成员函数。在这方面,我尝试运行本书中的练习,但最近遇到了以下错误 expected primary-expression before '.' token

错误:应在';之前使用主表达式';代币 我现在用C++来教自己C++,傻瓜都是一体的;第二版。为了创建这个程序,我使用Qt。我认为在头文件中组织对象和类是一种很好的做法,并在除main.cpp之外构建的.cpp文件中组织成员函数。在这方面,我尝试运行本书中的练习,但最近遇到了以下错误 expected primary-expression before '.' token,c++,qt,class,object,member-functions,C++,Qt,Class,Object,Member Functions,此错误发生在第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,

此错误发生在第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();
}
这是我的控制输入

#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
\ifndef控制输入
#定义控制输入
#包括
使用名称空间std;
类控制输入
{
公众:
int stringtonumber(字符串MyString);
字符串EnterPassword();
字符串enterOnlyNumbers();
};
#endif//控制输入

提前感谢您的反馈

您试图用类本身调用实例变量,就好像它们是静态的一样(这仍然是无效语法)。要使其正常工作,您需要一个
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();

}
intmain(intargc,char*argv[])
{
qcorea应用程序(argc、argv);
ControlInInput ctrlInput;//创建实例
...
输入的字符串=ctrlInput.EnterOnlyNumber();
int num=ctrlInput.StringToNumber(输入);

你不能像对待
静态
函数那样对待你的函数,但它们不是
静态
。你必须创建
控制输入的实例。
。啊;好吧,我明白了!这也是一件好事,因为我对创建对象的实例有错误的想法。我相信,因为对象包含在内,所以它被创建了。这很有魅力。谢谢你抽出时间。
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();

}