我想转发声明:
namespace boost {
namespace property_tree {
template<typename Key, typename Data, typename KeyCompare = std::less<Key> >
class basic_ptree;
typedef basic_ptree< std::string, std::string > ptree;
}
}
Run Code Online (Sandbox Code Playgroud)
但由于默认模板参数,我的g ++关于重新定义.我怎样才能做到这一点?
我在类中声明了一个类型为b的新指针时出现错误.请帮忙.
#include <iostream>
namespace B
{
class b;
}
class a
{
private:
B::b* obj_b;
public:
a(){}
~a(){}
void create()
{
b* obj_b = new b;
}
};
class b
{
private:
a *obj_a;
public:
b()
{
obj_a->create();
}
~b(){}
};
int main()
{
b obj;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我知道我们在C++中这样做:
int a();
int b() {
return a();
}
int a() {
return b();
}
Run Code Online (Sandbox Code Playgroud)
我怎么能在Java中做这样的事情?
有很多关于前向声明和不透明类型的问题,但大多数似乎是从库作者的角度来看的,或者人们试图使用没有指针的不完整类型等。
我正在使用一个接口接受/返回FOO *指针的库。我想确认我不能(或不应该)以某种方式向前声明FOO或FOO *在我的头文件中(它定义了一个带有FOO *成员的结构)。
我知道我可以只#include <library.h>在我的头文件和我的 .c 文件中,但由于这实际上只是一个学习项目,我想得到澄清。(一方面,前向声明似乎是可能的,因为我的结构成员只是一个指针,因此它的大小在不知道是什么的情况下是已知的FOO——但另一方面,我不知道它是否有效/聪明的typedef东西,FOO当库已经这样做。)
提前致谢!
在下面的代码中,这是调用 globals.cpp 命名空间中的变量实际全局变量。
全局变量.h
#ifndef GLOBALS_H_
#define GLOBALS_H_
namespace Constants
{
// forward declarations only
extern const double pi;
extern const double avogadro;
extern const double my_gravity;
}
#endif
Run Code Online (Sandbox Code Playgroud)
全局变量.cpp
namespace Constants
{
// actual global variables
extern const double pi(3.14159);
extern const double avogadro(6.0221413e23);
extern const double my_gravity(9.2); // m/s^2 -- gravity is light on this planet
}
Run Code Online (Sandbox Code Playgroud)
源代码.cpp
#include <iostream>
#include <limits>
#include "globals.h"
int main()
{
double value_of_pi = Constants::pi;
std::cout << value_of_pi; …Run Code Online (Sandbox Code Playgroud) 在我的程序的主文件中,我有以下声明
int main()
{
Customer c;
Part p;
Builder b;
auto partsVec = readpartFile();
auto customerVec = readcustomerFile();
auto builderVec = readbuilderFile();
fexists("Parts.txt");
complexity(c, partsVec);
robotComplexity(partsVec,customerVec);
writeFile(buildAttempt(b, complexity(c, partsVec), variability(customerVec, builderVec)));
}
Run Code Online (Sandbox Code Playgroud)
我的头文件由以下内容组成
vector<Part> readpartFile();
vector<Customer> readcustomerFile();
vector<Builder> readbuilderFile();
float complexity(const Customer& c, const std::vector<Part>& parts);
void robotComplexity(vector<Part> vecB, vector<Customer> vecC);
double variability(const vector<Customer>& customerList, const vector<Builder>& builderList);
vector<double> buildAttempt(Builder b, double variaiblity, double complexityRobot);
void writeFile(vector<double> build);
Run Code Online (Sandbox Code Playgroud)
除了 robotsComplexity 之外,所有功能都链接在一起。我在 main 中声明此函数会产生以下错误。
重载函数“robotComplexity”的多个实例与参数列表匹配: -- function "robotComplexity(const std::vector> &parts, const …
c++ linker-errors header-files forward-declaration function-signature
我正在研究这个C++项目.
所有类的实现都与.h文件分开.
但是,我不确定为什么/何时需要前方声明.
例如,当我#included"ClassType.h"时,我刚刚遇到错误,编译器完全拒绝编译具有指向ClassType的指针的类,即使类ClassType 在"ClassType.h"中明确定义.
为什么编译器不能简单地看到我已经#included"ClassType.h",为什么它需要一个前向声明呢?
#include "ClassType.h"
// REFUSES TO COMPILE WITHOUT forward declaration
class ClassType;
class SomeClass
{
ClassType* instance;
};
Run Code Online (Sandbox Code Playgroud) 在followint代码中,指针转换和多继承如何一起发挥作用?
class Foo {
public:
virtual void someFunc();
};
class Bar;
void someWork(Bar *bar) {
((Foo*) bar)->someFunc();
}
class Bar: public Zed, public Foo {
...
virtual void someFunc() { ... do something else ... }
}
Bar bar;
int main() {
someWork(&bar);
}
Run Code Online (Sandbox Code Playgroud)
我的理解有点不稳定.
一方面,有些工作对Bar一无所知,所以这不应该工作; 但另一方面,我已经向前宣布了Bar.
谢谢!
c++ virtual-functions multiple-inheritance forward-declaration
我的C++代码有一个小问题.
class Command {
public:
virtual void start(CommandDesc userinput, Converter* convertobj) = 0;
virtual void help(int option) = 0;
};
struct CommandDesc
{
std::string name;
std::string description;
Command* comobj; //Issue is here
};
Run Code Online (Sandbox Code Playgroud)
如果我在课前定义结构,我将无法定义成员
Command* comobj;
Run Code Online (Sandbox Code Playgroud)
如果我在类之后定义,我将无法将struct的实例传递给方法
virtual void start(CommandDesc userinput, Converter* convertobj) = 0;
Run Code Online (Sandbox Code Playgroud)
你能提出什么建议?有没有办法首先声明结构,而不是单独定义它?
我想转发声明头文件中的结构.
struct GLFWvidmode;
class DesktopVideoMode {
private:
const GLFWvidmode *videomode;
public:
DesktopVideoMode(const GLFWvidmode *videomode);
...
Run Code Online (Sandbox Code Playgroud)
在cpp文件中,我包含带有定义的外部头文件......
#include "DesktopVideoMode.hpp"
#include <GLFW/glfw3.h>
Run Code Online (Sandbox Code Playgroud)
...出现错误" Typedef重新定义不同类型('struct GLFWvidmode'与'GLFWvidmode') "的情况:
typedef struct
{
/*! The width, in screen coordinates, of the video mode.
*/
int width;
/*! The height, in screen coordinates, of the video mode.
*/
int height;
/*! The bit depth of the red channel of the video mode.
*/
int redBits;
/*! The bit depth of the green channel of the video mode. …Run Code Online (Sandbox Code Playgroud)