4

私は現在、ダミーオールインワン用のC++を使用して自分自身にC++を教えています。第2版​​。このプログラムを作成するために、私は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

フィードバックをお寄せいただきありがとうございます。

4

1 に答える 1

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();

}
于 2011-12-13T22:04:41.077 に答える