我有2个班级:DataObject和DataElement.DataObject保持指针(只)DataElementS,和一个DataElement包含指向几种类型,其中一个DataObject.
这曾经没有问题,因为我只使用指针DataObject输入DataElement,所以DataObject在标题中的前向声明DataElement就足够了.
但是,现在,我尝试添加一个析构函数DataElement,我需要删除一个DataObject.在这方面,编译器说:
dataelement/destructor.cc: In destructor ‘DataElement::~DataElement()’:
dataelement/destructor.cc:8: warning: possible problem detected in invocation of delete operator:
dataelement/destructor.cc:8: warning: invalid use of incomplete type ‘struct DataObject’
dataelement/dataelement.h:7: warning: forward declaration of ‘struct DataObject’
dataelement/destructor.cc:8: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined. …Run Code Online (Sandbox Code Playgroud) c++ destructor class-design circular-dependency forward-declaration
以下程序可以成功运行:
class Simple(object):
def __init__(self, name):
self.name = name
def __add__(self, other):
c = Composite()
c._members.append(self)
c._members.append(other)
return c
def __repr__(self):
return "Simple('%s')" % self.name
class Composite(object):
def __init__(self):
self._members = []
def __add__(self, obj):
if isinstance(obj, Simple):
out = Composite()
out._members = [k for k in self._members] + [obj]
elif isinstance(obj, Composite):
out = Composite()
out._members = [k for k in self._members + obj._members]
else:
raise TypeError
return out
if __name__ == "__main__":
s1 = Simple('one')
s2 = Simple('two') …Run Code Online (Sandbox Code Playgroud) python class-design forward-declaration method-resolution-order
我有两个类:A和B.类A的一些方法需要使用类B而相反(类B有需要使用类A的方法).
所以我有:
class A;
class B {
method1(A a) {
}
}
class A {
method1(B b) {
}
void foo() {
}
}
Run Code Online (Sandbox Code Playgroud)
一切正常.
但是当我尝试从B :: method1调用类A的foo()时:
class B {
method1(A a) {
a.foo();
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的结果是前向声明的编译错误和 不完整类型的使用.但为什么会这样呢?(我在使用之前已经宣布了A级?)
对不起,我是模板的新手,我搜索了很多,但我找不到解决方法如何声明转发模板(类的).
这是我的代码:
#ifndef CMAP_H
#define CMAP_H
#include "qvector.h"
class CMap
{
public:
CMap(const unsigned int & width, const unsigned int & height, const unsigned int & hexagonRadius);
CMap(const unsigned int & width, const unsigned int & height, const unsigned int & hexagonRadius, const QVector<QVector<unsigned int> > & landType);
~CMap();
private:
class Pimple;
Pimple * d;
};
#endif // CMAP_HRun Code Online (Sandbox Code Playgroud)
我想要的只是让#include"qvector.h"过时了.
我正在使用前向声明,现在我收到一个错误,指的是使用前向声明的类...所以fInstance forward声明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 // …Run Code Online (Sandbox Code Playgroud) 我正在使用OpenGL和DirectX,我已经开始为面向对象的游戏类开发基础知识.当前类的结构如下:
对象
--- | ---演员
--------- | ---典当
--------- | ---控制器
所以我有继承继承的类Pawn和Controller类.ActorObject
问题是我需要Controller在pawn类和Pawn控制器类中的实例中引用.
因此我向前宣布Pawn并Controller在Actor.h:
// Actor.h
// (...) Actor Declaration (...)
class Pawn;
class Controller;
Run Code Online (Sandbox Code Playgroud)
然后在Pawn.h:
// Pawn.h
class Pawn : public Actor
{
private:
Controller* controller;
public:
void DoSomethingWithController(void);
}
Run Code Online (Sandbox Code Playgroud)
这一切都很好,没有错误.问题是当我想访问该类中的成员时:
void Pawn::DoSomethingWithController(void)
{
// this->controller-> can't access members like this (members not found)
}
Run Code Online (Sandbox Code Playgroud)
那么,我该怎么做才能在Pawn中有一个Controller指针和一个指向我的Controller中的Pawn的指针,将它们保存在不同的文件(.h和.cpp)中,同时能够访问它的成员?
感谢您的时间.:d
[如果需要更多信息,我会提供]
我试图转发delcare这个嵌套的类,我已经尝试过但我没有工作.当我尝试转发声明我无法访问私人成员错误,所以我想我做错了什么.
#ifndef PLAYERHOLDER_H
#define PLAYERHOLDER_H
#include <QtCore>
#include <player.h>
#include <datasource.h>
class PLAYERHOLDER
{
private:
class CONTACTMODEL : public QAbstractTableModel
{
public:
explicit CONTACTMODEL(PLAYERHOLDER* holder);
int rowCount( const QModelIndex &parent ) const;
int columnCount( const QModelIndex &parent ) const;
QVariant data( const QModelIndex &index, int role ) const;
QVariant headerData( int section, Qt::Orientation orientation, int role ) const;
void update();
private:
static PLAYERHOLDER* m_playerHolder;
};
public:
static PLAYERHOLDER* getInstance();
void createPlayer(PLAYER *player);
void updatePlayer(int id);
void deletePlayer(int id);
PLAYER* findPlayer(int …Run Code Online (Sandbox Code Playgroud) 我一直在寻找我遇到的编译错误消息的答案,但我似乎我的用例更简单,并且这个问题甚至不应该存在.我当然缺少一些非常微不足道的东西,并希望找到错误的帮助.
我有以下代码片段.
/*file rand.h*/
class random{
// definition of class
};
Run Code Online (Sandbox Code Playgroud)
和另一个名为method.h的文件
/* file method.h*/
#include "rand.h"
/* lots of stuff...many lines */
class method{
random rng;
};
Run Code Online (Sandbox Code Playgroud)
最后一个cpp文件main.cpp
#include "method.h"
int main(){
method METHOD;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在编译时,我收到错误:
In file included from main.cpp:2:0:
method.h:40:5: error: ‘random’ does not name a type
random rng;
Run Code Online (Sandbox Code Playgroud)
method.h
#ifndef METHOD
#define METHOD
#include "rand.h"
class node
{
//stuff
};
// stuff
template<class T>
class ssa
{
public:
T& model;
random rng;
}; …Run Code Online (Sandbox Code Playgroud) int main ()
{
hello();
return 0;
}
int hello()
{
printf("\n hello world");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
根据C规则,下面定义的每个函数main()必须在上面声明,main() 但为什么它int作为返回类型的函数的异常?如果你将返回类型hello() 改为其他任何东西(void,char*等),它将抛出错误声明.为什么int 返回类型没有错误或警告?
如何在不使用C中的头文件的情况下编写hello world?
#include<conio.h>
#include<stdio.h>
void main(){
printf("Hello World");
getch();
}
Run Code Online (Sandbox Code Playgroud)
这是带头文件的简单C程序......
conio.h - 用于控制台stdio.h- 用于printf和scanfc++ ×7
c ×2
class-design ×2
class ×1
compilation ×1
declaration ×1
definition ×1
destructor ×1
function ×1
header-files ×1
member ×1
pointers ×1
python ×1
templates ×1