错误:预期在 '.' 之前有主表达式代币
我目前正在使用《A C++ for Dummies All-In One》自学 C++;第二版。为了创建这个程序,我使用 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 (我已经包括了我的头文件和 iostream 和 sstream
在这里,但由于某种原因,编辑器在这里给我带来了问题)
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
提前感谢您的任何反馈。
I am currently teaching myself C++ using A C++ for Dummies All-In-One; second edition. TO create this program I am using Qt. I understand it to be a good practice to organize objects and classes in your header files and prospectively your member functions in a .cpp file built in addition to the main.cpp. In this regard I try to run the exercises in this book as such but just recently encountered the following error.
expected primary-expression before '.' token
This error occurs on Lines 31, 32, and 37 so they appear to be relevant to my class member functions specifically.
My 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();
}
I did some research to find that this error indicates a pretty broad range of misuse of syntax. Unfortunately I was unable to find an instance that resembled mine specifically; I was hoping to get some insight from some of the more experienced programmers. If this is a simple issue that is on account of negligence on my end I apologize in advance and appreciate the feedback. I learn better if it gave me allot of trouble as opposed to a little..
Because these include my member functions I have also included my header file and .cpp
controlingInput.cpp (I have included my header file and iostream
and sstream
here but for some reason the editor was giving me problems on here)
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
And Here is my 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
Thanks in advance for any feedback.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试使用类本身调用实例变量,就好像它们是静态的一样(这仍然是无效的语法)。为了使其正常工作,您需要一个
ControlingInput
实例。You are attempting to call instance variables with the class itself as if they were static (which would still be invalid syntax). For this to work properly you need an instance of
ControlingInput
.