简体   繁体   中英

error: request for member which is of non class type

I am using forward declaration and now I am getting an error referring to the class that uses the forward declaration...so fInstance forward declares fConfig and then the Helper class (a namespace - used for global access to functions) - getting t

fConfig.h

#ifndef FCONFIG_H
#define FCONFIG_H

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <sys/types.h>
#include <fcntl.h>
#include "JWDSFork.h"
//#include "fInstance.h"

using namespace std;

class fConfig
{
    private:
        pid_t pid, w;

    public:
        pid_t cPid;
        string name;

        int group;
        int instanceId;
        int numInstance;
        int tries;

        bool reply;
        bool debug;
        bool service;
        bool currentlyRunning;

        time_t startTime;
        time_t endTime;

        string path;

        fConfig();
        virtual ~fConfig();

        void start();
        string intToString(int);
        char* stringToChar(string);
};

#endif // FCONFIG_H

fInstance.h

#ifndef FINSTANCE_H
#define FINSTANCE_H

//#include "fConfig.h"
#include <vector>
#include <sys/types.h>
#include <string>

using namespace std;

class fConfig;


class fInstance
{
public:

    fConfig* config;
    pid_t pid;
    vector<string> notes;
    vector<time_t> times;

    fInstance();
    virtual ~fInstance();


};

#endif // FINSTANCE_H

Helper.h

#ifndef HELPER_H
#define HELPER_H

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <sstream>
#include <limits.h>
#include "fInstance.h"

using namespace std;

namespace Helper
{
    extern string APPDIR;

    bool errorCheck(int, char*);
    string charToString(char*, int);
    string longToString(unsigned long);
    bool Contains(vector<fInstance>, fInstance);
    string convertInt(int);
    string convertDouble(double);
    bool Read(int, char*, size_t);
    bool Write(int, char*, size_t);
};

#endif // HELPER_H

Helper.cpp

//Helper.cpp - function that causes a problem
#include "Helper.h"
namespace Helper
{

bool Contains(vector<fInstance> a, fInstance b)
    {
        for(unsigned int i= 0; i < a.size(); i++ )
        {
            if(a[i].config.name == b.config.name)
            {
                return true;
            }
        }

        return false;
    }
}

I am getting these errors

error: request for member ‘name’ in ‘a.std::vector<_Tp, _Alloc>::operator[] [with _Tp = fInstance, _Alloc = std::allocator<fInstance>](((long unsigned int)i))->fInstance::config’, which is of non-class type ‘fConfig*’

那是一条非常不友好的错误消息,但是它的意思是config成员是一个指针,因此您需要使用->运算符,即。

 if(a[i].config->name == b.config->name)

Assuming that you have an operator== overloaded for your type fInstance , you can write your function as (note also that you should pass your parameters a and b by reference-to-const)

#include<algorithm>

bool fInstance::operator==(const fInstance& other) 
{ 
    return config->name == other.config->name; 
}

bool Contains(const vector<fInstance>& a, const fInstance& b)
{
    return std::find_if(a.begin(), a.end(), b);
}

If you don't have an operator== in your fInstance class, you can use a C++11 lambda expression

bool Contains(const vector<fInstance>& a, const fInstance& b)
{
    return std::find_if(a.begin(), a.end(), 
    [](const fInstance& i) { return i.config->name == b.config->name; });
}

And even better yet, you should encapsulate the name member into a member function of fInstance :

std::string fInstance::name() { return config->name; };

bool Contains(const vector<fInstance>& a, const fInstance& b)
{
    return std::find_if(a.begin(), a.end(), 
    [](const fInstance& i) { return i.name() == b.name(); });
}

This increases encapsulation, decreases compilation times and makes the implementation of the fInstance class opaque to its clients. Your current implementation leaves the fConfig implementation transparant to clients. This decrease in encapsulation is called a violation of the Law of Demeter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM