Visual 2010一直告诉我"错误:表达式必须具有类类型"

Cra*_*unt 12 c++ visual-studio-2010

好的,我需要一些见解.

我正在学习C++课程,正在开展第二个项目.我正在尝试创建一个选项列表,允许您将字符串向量存储在电子邮件中.

现在在花时间帮助我并查看代码之前我想指出我的问题.我在文件"HughesProject2-1.cpp"中创建了一个对象:

HughesEmail myhughesEmail();
Run Code Online (Sandbox Code Playgroud)

好吧,问题出现在我使用此对象运行displayList()之后:

myHughesEmail.displayList();
Run Code Online (Sandbox Code Playgroud)

Visual 2010一直告诉我"错误:表达式必须具有类类型"

现在我正在使用这本书作为参考,他们以同样的方式创建了一个对象,并在之后以相同的方式使用它.我对我的错误感到困惑,因为我的文件与使用对象的基本知识和正在完成的工作完全不同.我理解其他错误,因为这是不完整的,我还在学习,我需要知道在我制作之后最有可能导致我使用该对象.提前致谢.

我有三个文件:

HughesEmail.cpp

// Classes for HughesProject2-1.cpp and HughesEmail.h

// Includes
#include <string>
#include <iostream>
#include <vector>
#include <iomanip>
#include "HughesEmail.h"

// Namespaces
using namespace std;

// Initializing Constructor
HughesEmail::HughesEmail()
{
    vector< string > emailStorage( 100 );
    emailMinimumLength = 9;
    exitOption = 1;
    emailOption = 1;
}

void HughesEmail::displayList()
{
    // Check if exit is set, if not run.
    if ( exitOption == 1 )
    {
    // Email list options
    cout << "Choose from the list of options: \n"
        "1 - Store an email address.\n"
        "2 - Search an email address.\n"
        "3 - List all email adresses.\n"
        "4 - Delete an email address.\n"
        "0 - Exit.\n" << endl;

    while ( emailOption != 0 )
    {
        // Get user input for email list option
        cout << "Option? : ";
        cin >> option;

        switch ( option )
        {
        case '0':
            // set exitOption to 0
            exitOption = 0;
            emailOption = 0;
            break;
        case '1':
            //Input email name
            cout << "Please input email to be stored: " << endl;
            cin >> emailName;
            // run storeEmail
            storeEmail( emailName );
            break;
        case '2':
            // run searchEmail

            break;
        case '3':
            // run listEmail

            break;
        case '4':
            // run deleteEmail

            break;

        //Ignore
        case '\n':
        case '\t':
        case ' ':
            break;

        default:
            cout << "\nPlease choose a valid option." << endl;
            break;
        } // end switch

    } // end while

    } else {

        exitOption = 0;

    } // end else
}


void HughesEmail::storeEmail( string emailName )
{
    // Initialize counter
    int i;
    i = 0;

    // Check if input emailName meets emailMinimumLength
    if( emailName.length() >= emailMinimumLength )
    {

      // if email in vector slot i is less than minimum length, then override with new email.
      if ( emailStorage[ i ].length() < emailMinimumLength )
      {
          emailStorage[ i ] = emailName;
      } else {
          i++;
      } // end else

    } else {
        cout << "Email does not meet the minimum length of: " << emailMinimumLength << " characters." << endl;
    } // end else
}
Run Code Online (Sandbox Code Playgroud)

HughesEmail.h

 // In this project: HughesProject2.h
    // Class header file.

    //Includes
    #include <string>
    #include <iostream>
    #include <vector>

    //Namespaces
    using namespace std;

    class HughesEmail
    {
    public:
        HughesEmail();
        void displayList();
        void storeEmail( string );
        string searchEmail( string );
        string listEmail();
        void deleteEmail();
    private:
        vector< string > emailStorage;
        int emailMinimumLength;
        int emailOption;
        int exitOption;
        char option;
        string emailName;
    };
Run Code Online (Sandbox Code Playgroud)

HughesProject2-1.cpp

// In this project: HughesProject2-1.cpp
// Class creation to store email adresses. Adding, deleting, searching and listing email addresses.

// Includes
#include <string>
#include <iostream>
#include <vector>
#include "HughesEmail.h"

// Namespaces
using namespace std;

int main()
{
    //Create HughesEmail Object
    HughesEmail myHughesEmail();
    myHughesEmail.displayList();

}
Run Code Online (Sandbox Code Playgroud)

Pup*_*ppy 39

你遇到了一个叫做最令人烦恼的解析的东西.

HughesEmail myHughesEmail();
Run Code Online (Sandbox Code Playgroud)

此行不会HughesEmail在堆栈上构造新对象.相反,它声明了一个返回a HughesEmail并且不执行任何操作的函数.你应该删除空括号.