C++: 'Foo' 中成员 'f' 的错误请求,它是非类类型 'Foo*'

标签 c++ oop compiler-errors

我知道以前有人问过这个问题。我已经尝试实现答案,但仍然遇到此错误。如果有人有任何建议,将不胜感激。下面是主文件、头文件和源文件。如果您需要任何其他信息,请告诉我。谢谢!

主要.cpp

/* 
 * File:   main.cpp
 * Author: agoodkind
 *
 * Reads in a .list file from IMDB
 * Prompts user for Actor or Movie lookup
 * Iterates through file
 * Returns results of query
 * 
 */

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

#include "ActorResume.h"

using namespace std;


// function prototypes
void printmenu(void);
void createMovieList(fstream &infile);
void movieCastLookup(void);
void costarLookup(ActorResume &, fstream &infile);

int main() {    

    char choice;                //user menu selection
    bool not_done = true;       // loop control flag

    // create input file stream
    fstream infile;
    // CHANGE BACK TO NON-TEST!! 
    infile.open("/Users/agoodkind/Documents/CUNY/Analysis_and_Design/Assignments/Assignment1/actorsTEST.2010.list", ios::in);


    do {
        printmenu();
        cin >> choice;
        switch(choice)
        {
            case 'q':
            case 'Q':
                infile.close();
                not_done = false;
                break;
            case 'a':
            case 'A':
                createMovieList(infile);
                break;
            case 'm':
            case 'M':
                movieCastLookup();
                break;
            default:
                cout << endl << "Error: '" << choice << "' is an invalid selection -  try again" << endl;
                break;
        } //close switch

    } // close do() loop

    // exit program
    while (not_done);
    return 0;
} // close main()

/* Function printmenu()
 * Input:
 *  none
 * Process:
 *  Prints the menu of query choices
 * Output:
 *  Prints the menu of query choices
 */
void printmenu(void) {
    cout << endl << endl;
    cout << "Select one of the following options:" << endl;
    cout << "\t****************************" << endl;
    cout << "\t    List of Queries         " << endl;
    cout << "\t****************************" << endl;
    cout << "\t     A -- Look Up Actors' Costars" << endl;
    cout << "\t     M -- Movie Cast List" << endl;
    cout << "\t     Q -- Quit" << endl;
    cout << endl << "\tEnter your selection: ";
    return;    
}

/* Function costarLookup()
 * Input:
 *      Actor name
 * Process:
 *      Looks up all actors also in actor's movieList
 * Output:
 *      Prints list of costars
 */
void createMovieList(fstream &infile) {

    string actorName; // actor's name to be queried

    cout << endl << "Enter Actor Name: ";
    cin >> actorName;

    ActorResume *ar = new ActorResume(actorName);

    // add movie list to ActorCostars ADT created above
    // string for readline()
    string line;

    if (infile.is_open()) { //error checking to ensure file is open
        infile.seekg(0, ios::beg); // to be safe, reset get() to beginning of file
        while (infile.good()) { //error checking to ensure file is being read AND not end-of-file

            bool actor_found = false; // checks if actor name has been found
            getline(infile,line);

            if (line == actorName) {
                actor_found = true;
            }

            // add movies to actor's movieList
            while (actor_found && line[0] == '\t') {
                ar.addMovie(line);
            } 
        }
    }

    costarLookup(*ar, infile);
    delete ar;

} // close costarLookup()


void costarLookup(ActorResume actRes, fstream &infile) {

    // string for readline()
    string line;

    // vector to store costar names
    vector<string> costars;

    if (infile.is_open()) {             // error checking to ensure file is open

        infile.seekg(0, ios::beg);      // reset get() to beginning of file
        while (infile.good()) {         // error checking to ensure file is being read AND not end-of-file

            // while looping through file, store each actor's name
            string actorName;

            getline(infile,line);

            // check if first character is an alphanumeric character
            if (line[0] != '\t' && line[0] != NULL) {
                actorName = line;
            }

            // add actors name to list of costars
            if (actRes.movieInList(line)) {
                costars.push_back(actorName);
            }
        }
    }
    if (costars.size() == 0) {
        cout << "No costars found";
    }
    else if (costars.size() > 0) {
        cout << "Costars of " << actRes.getActorName() << ": " << endl;
        for (int i = 0; i < costars.size(); i++) {
            cout << "* " << costars[i];
        }
    }
}

ActorResume.h

/* 
 * ActorResume Specification Class
 */

#ifndef ACTORRESUME_H
#define ACTORRESUME_H

#include <iostream>
#include <string>
#include <vector>
#include <cstring>

using namespace std;

// ActorResume declaration
class ActorResume {

private:
    string actorName;
    vector<string> movieList;

public:

    //default constructor
    ActorResume() {
//        actorName = NULL;
        movieList.clear();
    }

    //typical constructor implemented for creating ActorResume
    ActorResume(string aName) {
        actorName = aName;
        movieList.clear();
    }

    // member functions
    void addMovie(string);
    string getActorName();
    bool movieInList(string);
//    void printMovieList();

};


#endif  /* ACTORRESUME_H */

Actor 简历.cpp

// ActorResume Implementation

#include <iostream>
#include <string>
#include <vector>
#include <cstring>

#include "ActorResume.h"

/*
 * ActorResume function addMovie()
 * Input:
 *      Movie Name
 * Process:
 *      adds movie to movieList
 * Output:
 *      None
 */
void ActorResume::addMovie(string movie) {
    movieList.push_back(movie);
//    return;
}

/*
 * ActorReusme function getActorName()
 * Input:
 *      None
 * Process:
 *      returns actor's name
 * Output:
 *      String of actor's name
 */
string ActorResume::getActorName() {
    return actorName;
}

/*
 * ActorResume function movieInList()
 * Input:
 *      string to search for
 * Process:
 *      searches movieList vector for string
 * Output:
 *      If movie is found, true
 *      Else, false
 */
bool ActorResume::movieInList(string movie) {
    for(size_t i = movieList.size() - 1; i != (size_t)-1; i--) {
        if (movieList[i] == movie) {
            return true;
        }
    }
    return false;
}

错误:

main.cpp: In function 'void createMovieList(std::fstream&)':
main.cpp:124: error: request for member 'addMovie' in 'ar', which is of non-class type 'ActorResume*'

最佳答案

Error request for member 'Foo' in 'f', which is of non-class type 'Foo*'

错误告诉您所有您需要知道的。

ActorResume *ar = new ActorResume(actorName);

ar 是指向 ActorResume 对象的指针,而不是 ActorResume 对象本身。所以……

ar.addMovie(line);

应该是

ar->addMovie(line);

您使用 -> 运算符来取消引用指针并访问它所引用的对象的成员,而不是点运算符。指针没有成员。

关于C++: 'Foo' 中成员 'f' 的错误请求,它是非类类型 'Foo*',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12361350/

相关文章:

c++ - 这可能吗?(C++)

c++ - 将 COM 接口(interface)指针传递给函数

c++ - C++ 容器的问题

c++ - 合并数字以形成一个大数字

Java初学者手动输入值并打印对象void main的详细信息

c# - 如何在 C# 静态和非静态方法之间做出决定?

php - OOP 内聚/耦合困惑

c# - CS0411 : The type arguments for method X cannot be inferred from the usage when building with TeamCity

.net - 从VS 2008和.NET3.5迁移到VS 2010和.NET4.0时生成错误

linux - Glibc-2.22 make(无限循环)错误 [LFS 7.8 - 6.9]