C++ Visual 2010 一直告诉我“错误:表达式必须具有类类型”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5167555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:34:55  来源:igfitidea点击:

Visual 2010 keeps telling me "error: Expression must have class type"

c++visual-studio-2010

提问by CrazyGrunt

Okay I need some insight.

好吧,我需要一些洞察力。

I am taking a C++ class and am working on my second project. I am trying to create a list of options that allow you to store emails in a string vector.

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

Now before taking the time to help me and look at the code I want to point out my problem. I made an object in the file "HughesProject2-1.cpp":

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

HughesEmail myhughesEmail();

Well the problem comes right after this when I used this object to run displayList():

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

myHughesEmail.displayList();

Visual 2010 keeps telling me "error: Expression must have class type"

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

Now I am using the book as a reference to doing this and they created an object the same way and used it the same way right after. I am confused at what I have wrong as my file is quite different past the basics of using the objects and what is being done. I understand there my be other errors as this is incomplete and I'm still learning, I need to know what is most likely causing me from using the object after I made it. Thanks in advance.

现在我用这本书作为参考来做这件事,他们以同样的方式创建了一个对象,然后以同样的方式使用它。我对自己的错误感到困惑,因为我的文件与使用对象的基础知识和正在执行的操作完全不同。我知道我还有其他错误,因为这是不完整的,我仍在学习,我需要知道是什么最有可能导致我在创建后使用该对象。提前致谢。

I have three files:

我有三个文件:

HughesEmail.cpp

休斯电子邮件.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
}

HughesEmail.h

休斯电子邮件.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;
    };

HughesProject2-1.cpp

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

}

回答by Puppy

You've run into something called the most vexing parse.

您遇到了所谓的最令人头疼的解析。

HughesEmail myHughesEmail();

This line does not construct a new HughesEmailobject on the stack. Rather, it declares a function that returns a HughesEmailand takes nothing. You should remove the empty parentheses.

此行不会HughesEmail在堆栈上构造新对象。相反,它声明了一个返回 aHughesEmail并且什么都不带的函数。您应该删除空括号。