0

我正在使用前向声明,现在我收到一个错误,指的是使用前向声明的类......所以 fInstance 前向声明 fConfig,然后是 Helper 类(命名空间 - 用于对函数的全局访问) - 得到 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

助手.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

助手.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*’
4

2 回答 2

7

这是一个非常不友好的错误消息,但这意味着该config成员是一个指针,因此您需要改用->运算符,即。

 if(a[i].config->name == b.config->name)
于 2012-05-11T20:28:37.350 回答
3

假设您有一个 operator== 为您的 type 重载fInstance,您可以将您的函数编写为(另请注意,您应该传递参数ab通过引用到 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 表达式

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实施对客户透明。这种封装的减少被称为违反得墨忒耳定律

于 2012-05-11T20:34:12.387 回答