错误:请求非类型的成员

时间:2012-05-11 20:24:43

标签: c++ forward-declaration

我正在使用前向声明,现在我收到一个错误,指的是使用前向声明的类...所以fInstance forward声明了fConfig然后是Helper类(一个命名空间 - 用于全局访问函数) - 获取吨

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;
    }
}

我收到这些错误

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*’

2 个答案:

答案 0 :(得分:7)

这是一个非常不友好的错误消息,但这意味着config成员是一个指针,所以你需要使用->运算符,即。

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

答案 1 :(得分:3)

假设您的类型fInstance有运算符==重载,您可以将函数编写为(请注意,您应通过引用传递参数ab - 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);
}

如果您的operator==课程中没有fInstance,则可以使用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; });
}

更好的是,您应该将name成员封装到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(); });
}

这增加了封装,减少了编译时间,并使fInstance类的实现对其客户端不透明。您当前的实现将fConfig实现透明留给了客户端。封装的减少被称为违反Law of Demeter