- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前正在使用 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
提前感谢您的任何反馈。
最佳答案
您正试图用类本身调用实例变量,就好像它们是静态的一样(这仍然是无效语法)。为了使其正常工作,您需要一个 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();
}
关于c++ - 错误 : expected primary-expression before '.' token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8496775/
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我是脚本新手。如何编写 Expect 脚本以通过 ssh 连接到设备并提示用户输入密码?我们使用 pin + RSA token 代码作为密码,因此我无法存储密码。 #!/usr/bin/expect
我编写了以下代码并尝试执行它。但我在执行 do {”时遇到“无效的命令名称“do”” 代码: #!/usr/bin/expect set val 0; set input 5; do { pu
我已经查看了 Expect 联机帮助页并用 Google 搜索了它,但我还没有找到 expect 的 -r 是什么。我看到这个选项以前是这样用的 expect -r "\r\n\r\n" 在 expe
我的 shebang 看起来像这样: #!/usr/bin/expect -d 当我从命令行运行脚本时,它会提供我想要的内容。 但是,我通过 crontab 运行这个脚本。是否可以将调试开关保持打开状
我是 Expect 脚本的新手。 我在 Linux 机器上为 ssh 编写了一个 Expect 脚本,在那里我在 ssh 到不同的 Linux 机器时遇到了问题。下面我复制了脚本。 !/usr/loc
Scene 1, Layer 'script', Frame 1, Line 9 1084: Syntax error: expecting identifier before this. Sc
我正在运行调试命令以将命令的输出记录到文件中。我尝试了 log_file 命令,但它没有记录输出。我的代码如下: log_file -a gdb.txt send "~/debugulator.sh
我希望 expect_user 有一个无限的(或非常大的)超时和 expect 的默认超时。有没有办法设置不同的超时?或者我是否只需要在每次更改用途之前手动执行此操作? 最佳答案 expect 和ex
我正在学习 iOS 编程(我来自 Android),我正在寻找更容易获取字符串的方法。有了这个建议,我定义了下一个宏并在一些代码片段中使用它: #define STRING_BASE @"InfoPl
你好我是 rspec 的新手,我想弄清楚将 block 传递给 expect{} 和只使用 expect() 之间的区别 这是一个简单的例子 require "rails_helper" RSpec.
我正在尝试为 React JS 运行单元测试 - 使用 jest/enzyme。 目前测试失败。不太清楚为什么,也许我没有正确调用 expect(wrapper.find)。这是我测试的一部分: F
例如,现在我有一个“root.exp”期望脚本如下: spawn ssh user@ip expect "Password:" send "password" 然后,我要发送到这个ssh服务器的exp
您好,我是 Expect 脚本编写的新手,我一直在尝试使用以下方法将 IP 地址获取到变量中: set timeout -1 spawn $env(SHELL) match_max 100000 se
expect.anything() 不适用于 expect.toBe(),但适用于 expect.toEqual() test("this will pass", () => { expect("
我有一个如下所示的简单脚本,从命令行读取 2 个数字并将它们加在一起: $cat runexp.sh #!/bin/bash echo "read 1st number" read n1 echo "
当 Linux 机器的 $IP 登录后询问密码时,下面的 expect 脚本工作正常 但在某些情况下,某些Linux机器不需要ssh密码(我们可以不用密码登录), 所以我需要更改我的期望脚本以支持没有
我正在尝试使用 expect 远程登录服务器并更改用户密码。该应用程序要求,如果您要更改的密码包含特殊字符,则将其引用。问题是,还需要引用 expect send 语句,当我尝试将两者结合起来时,脚本
下面这个简单的 expect 脚本的目标是获取远程机器上的 hostname 名称 有时期望脚本无法执行到 $IP_ADDRESS 的 ssh(因为远程机器不活动等) 所以在这种情况下,expect
我试图创建一个宏来替换, first: Some(first.as_ref().parse::().expect("Could not parse 'first'")) 我在其他模块(如 Clap w
我是一名优秀的程序员,十分优秀!