C++ 错误:请求非类类型的成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10558156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: request for member which is of non class type
提问by JonH
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
我正在使用前向声明,现在我收到一个错误,指的是使用前向声明的类......所以 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
实例.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
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
//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*'
回答by Peter
That's a pretty unfriendly error message, but what it means is that the config
member is a pointer, so you need to use the ->
operator instead, ie.
这是一个非常不友好的错误消息,但这意味着该config
成员是一个指针,因此您需要改用->
运算符,即。
if(a[i].config->name == b.config->name)
回答by TemplateRex
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)
假设您的 type 有一个 operator== 重载fInstance
,您可以将函数编写为(另请注意,您应该传递参数a
并b
通过引用到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
如果您operator==
的fInstance
班级中没有 an ,则可以使用 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; });
}
And even better yet, you should encapsulate the name
member into a member function of fInstance
:
更好的是,您应该将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(); });
}
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.
这增加了封装,减少了编译时间并使fInstance
类的实现对其客户端不透明。您当前的实现使fConfig
实现对客户透明。这种封装的减少被称为违反了得墨忒耳定律。