C++ 错误:请求非类类型的成员

C++ 错误:请求非类类型的成员,c++,forward-declaration,C++,Forward Declaration,我正在使用前向声明,现在我在引用使用前向声明的类时遇到了一个错误…因此fInstance forward声明了fConfig,然后Helper类声明了一个名称空间(用于全局访问函数)获取 fConfig.h fInstance.h 助手 Helper.cpp 我得到了这些错误 error: request for member ‘name’ in ‘a.std::vector<_Tp, _Alloc>::operator[] [with _Tp = fInstance, _Alloc

我正在使用前向声明,现在我在引用使用前向声明的类时遇到了一个错误…因此fInstance forward声明了fConfig,然后Helper类声明了一个名称空间(用于全局访问函数)获取

fConfig.h

fInstance.h

助手

Helper.cpp

我得到了这些错误

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

这是一条非常不友好的错误消息,但它的意思是配置成员是指针,因此您需要使用->操作符,即

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

假设您的类型fInstance有一个操作符==重载,那么您可以编写函数,注意您应该通过引用const来传递参数a和b

#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);
}
更好的是,您应该将name成员封装到fInstance的成员函数中:


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

ifa[i].config->name==b.config->name?在任何情况下,你只是在重新设计这里,这是不好的…当我这样做的时候…我最终不得不将所有请求config->some_变量的变量更改为指针…所以在做了所有这些之后-它最终给出了错误:“struct fConfig”@JonH的正向声明-在另一种情况下,您可以将fInstance更改为保持anfConfig而不是fConfig*。任何一种方法都由您决定,但如果是指针,则必须使用->;如果它是一个必须使用的对象..Jon:Helper.cpp需要包含fConfig.h,如果它与fConfig结构的成员有任何关系。当我更改所有内容以使用指针时…我最终不得不将请求config->some_变量的所有变量更改为指针…所以在完成所有这些操作后-它最终给出错误:forward声明“StuttFCOFIGG”@ JONH,请参阅C++部分,了解指针对数据类型的更多信息。特别是,您可能希望在FIFTRONT类中向前声明FCONFIG。@ JonH You可能会查看第二个最被投票的问题:得到有效C++的副本。“指向实现的指针——或pimpl——习语在这本书的一半左右都有介绍。@rhalbersma:如果可以的话,我想+2这个注释和答案。虽然运算符==方法很简洁,但我还是更喜欢lambda,除非通过config->name比较类是比较它们的明显方式。我曾经被带有重载运算符的类咬过,这些类允许与STL算法和容器一起使用,但不一定能达到预期效果。
//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*’
 if(a[i].config->name == b.config->name)
#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);
}
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; });
}
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(); });
}