根据这个问题,如果所有构造函数和析构函数都不是内联的(则需要完全定义的类型),则可以转发声明智能指针。当未提供析构函数时,编译器将声明一个析构函数并提供内联定义,该定义随后要求标头中完全已知智能指针中的类型。这同样适用于默认构造函数。
然而,我发现它也适用于继承的构造函数,这对我来说有点令人困惑。考虑:
class Base
{
public:
Base(); //defined in cpp
};
class SomeClass;
class Derived : public Base
{
using Base::Base;
~Derived(); //defined in cpp
std::unique_ptr<SomeClass> ptr;
};
Run Code Online (Sandbox Code Playgroud)
Derived除非显式声明构造函数并仅在源文件中定义,否则不会编译。为什么?构造函数Base不是内联的,据我所知,using 指令应该以与其他成员类似的方式导致构造函数的“继承”。或者编译器是否将其解释为“为我声明与中相同的构造函数Base并内联定义它们”?
如何在 PL/SQL 匿名块中定义包含自身集合属性的记录类型?看下面的例子:
DECLARE
type t_item is record (
name varchar2(64),
children t_items -- referencing t_items type
);
type t_items is table of t_item; -- referencing t_item type
BEGIN
-- script code
END
Run Code Online (Sandbox Code Playgroud)
PL/SQL 没有类型提升,因此 Oracle 引擎会引发异常:
PLS-00498:在声明之前非法使用类型
如何定义属性中t_item包含 a 的记录?table of t_itemchildren
考虑以下代码:
namespace A
{
class B
{
protected:
friend class C;
static void foo();
};
}
class C
{
public:
C() { A::B::foo(); }
};
int main()
{
C c;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
按照当前构造,此代码将无法编译 - 中 声明的友谊class B适用于 a (当前不存在)A::C,而不是C全局命名空间中的 the 。假设我无法添加C到非全局命名空间,我如何有效地解决这个问题?我尝试过使用friend class ::C;,但编译器不喜欢这样。我也尝试过在范围class C;之前进行向前声明namespace A,但这似乎也不起作用。
有一个类 A,所有声明和定义都放在里面,如下所示:
class A
{
void f(); // forward declaration for a lengthy method
...
void g()
{
f(); // call the above forward-declared method
}
void f() // definition of a lengthy method
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
我不想像 A::f() 那样将 f() 从类头中取出,但只想将所有源保留在类中。但是,当我编译它时,出现以下错误:
error C2535: 'void A::f()': member function already defined or declared
note: see declaration of 'A::f'
Run Code Online (Sandbox Code Playgroud)
难道就没有办法解决这个问题吗?
在处理包括彼此在内的多个类时,我遇到了这个错误:
error: expected class-name before '{' token
Run Code Online (Sandbox Code Playgroud)
我看到发生了什么,但我不知道如何正确纠正它.这是代码的抽象版本:
啊
#ifndef A_H_
#define A_H_
#include "K.h"
class A
{
public:
A();
};
#endif /*A_H_*/
Run Code Online (Sandbox Code Playgroud)
A.cpp
#include "A.h"
A::A() {}
Run Code Online (Sandbox Code Playgroud)
BH
#ifndef B_H_
#define B_H_
#include "A.h"
class B : public A
{ // error: expected class-name before '{' token
public:
B();
};
#endif /*B_H_*/
Run Code Online (Sandbox Code Playgroud)
B.cpp
#include "B.h"
B::B() : A() {}
Run Code Online (Sandbox Code Playgroud)
JH
#ifndef J_H_
#define J_H_
#include "B.h"
class J
{
public:
J();
};
#endif /*J_H_*/
Run Code Online (Sandbox Code Playgroud)
J.cpp
#include "J.h"
J::J() {} …Run Code Online (Sandbox Code Playgroud) 我的班级遇到了一些问题,因为它们彼此依赖,如果没有宣布另一个,就不能宣布.
class block: GtkEventBox {
public:
block(board board,guint x,guint y): image("block.png") {
this.board = board;
this.x = x;
this.y = y;
board.attach(this,x,y,x+1,y+1);
}
void move(guint x,guint y) {
board.remove(this);
this.x = x;
this.y = y;
board.attach(this,x,y,x+1,y+1);
}
private:
guint x, y;
board board;
GtkImage image;
};
class board: Gtk::Table {
public:
board(): Gtk::Table(25,20) {
blocks_c = 0;
}
void addBlock(guint x,guint y) {
blocks_a[blocks_c++] = new block(this,x,y);
}
private:
block* blocks_a[24];
int blocks_c;
};
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,"块"类需要知道"板"是什么,反之亦然.提前致谢!
我刚刚开始使用C++,所以我正在寻找一些代码来学习.我在Breakout游戏中找到了这段代码片段.
#pragma once
#include "force.hpp"
#include "brick.hpp"
#include <vector>
class Painter;
class Ball;
class Wall
{
public:
enum { ROWS_COUNT = 16,
COLS_COUNT = 8 * 3 };
enum { WIDTH = ROWS_COUNT * Brick::WIDTH,
HEIGHT = COLS_COUNT * Brick::HEIGHT };
Wall();
void draw(Painter &) const;
Force tick(const Ball &);
public:
typedef std::vector<Brick> Bricks;
Bricks bricks_;
};
Run Code Online (Sandbox Code Playgroud)
我不理解的唯一部分如下:
class Painter;
class Ball;
Run Code Online (Sandbox Code Playgroud)
两个"班级[名字];"是什么意思?在源代码中有不同的Painter.cpp,Painter.hpp,Ball,hpp,Ball.cpp.
这意味着某种包括?
当您包含一些文件时发生了什么,而当您向前声明某些函数/类时发生了什么?如果两个文件包含相同文件,则第一个文件将成功读取所有功能,第二个文件将失败,但仍能够使用这些功能?
当我向前声明一些功能时会发生什么?该功能现在“保存”了吗,我可以在任何地方使用它,或者仅在同一文件中知道它?那为什么两个带有include(到带有守卫的文件)的文件将起作用?
我可以将所有内容都包含在主体中而不再打扰吗?
编辑:
以及为什么cpp文件应包含其标题?如果我不包括这些怎么办?
我正在尝试将StackMob添加到我的项目中.它表示SMClient在将SDK拖动到项目后创建实例,选中"为...创建组"并添加到目标.我按照这些步骤.
但是,当我创建我的SMClient和SMCoreDataStore实例时,它给了我一个错误Receiver 'SMClient' for class message is a forward declaration和相同的错误SMCoreDataStore.这是我的代码:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@class SMClient;
@class SMCoreDataStore;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (strong, nonatomic) SMCoreDataStore *coreDataStore;
@property (strong, nonatomic) SMClient *client;
@end
Run Code Online (Sandbox Code Playgroud)
我的一部分.m:
#import "AppDelegate.h"
#import "StackMob.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.client = [[SMClient alloc] initWithAPIVersion:@"0" publicKey:@"YOUR_PUBLIC_KEY"];
self.coreDataStore …Run Code Online (Sandbox Code Playgroud) 例:
@class MyRootObject;
@interface MyObject : MyRootObject
@end
Run Code Online (Sandbox Code Playgroud)
获取表单XCode:
Class MyObject defined without specifying a base class.
Run Code Online (Sandbox Code Playgroud)
MyRootObject类是:
@interface MyRootObject : NSObject
- (id)init;
@end
@implementation MyRootObject
- (id)init
{
self = [super init];
if(self){
// some code here
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud) c++ ×7
objective-c ×2
appdelegate ×1
c++11 ×1
c++17 ×1
constructor ×1
friend ×1
header ×1
hoisting ×1
include ×1
inheritance ×1
ios ×1
namespaces ×1
oracle ×1
plsql ×1
xcode ×1