我有Preprocessor.h
#define MAX_FILES 15
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
typedef struct Preprocessor Prepro;
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
Run Code Online (Sandbox Code Playgroud)
我意识到我必须将声明与定义分开.所以我创建了Preprocessor.c:
#define MAX_FILES 15
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
typedef struct Preprocessor Prepro;
Run Code Online (Sandbox Code Playgroud)
而Preprocessor.h现在是:
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
Run Code Online (Sandbox Code Playgroud)
显然,这是行不通的,因为Pr..h不知道Prepro类型.我已经尝试过几种组合,但都没有.我找不到解决方案.
所以我有一个A类,我想调用一些B类函数.所以我加上"bh".但是,在B类中,我想调用一个A类函数.如果我包含"啊",它会以无限循环结束,对吧?我能做些什么呢?
当我在头文件中声明一个函数,并将该函数的定义放在其他文件中时,编译器/链接器如何找到定义?它是否系统地搜索其路径中的每个文件,还是有更优雅的解决方案?这一天一直困扰着我,我一直无法找到解释.
以下是无效代码:
struct foo {
struct bar;
bar x; // error: field x has incomplete type
struct bar{ int value{42}; };
};
int main() { return foo{}.x.value; }
Run Code Online (Sandbox Code Playgroud)
这很清楚,因为foo::bar
在foo::x
定义的点上被认为是不完整的.
但是,似乎有一个"解决方法"使相同的类定义有效:
template <typename = void>
struct foo_impl {
struct bar;
bar x; // no problems here
struct bar{ int value{42}; };
};
using foo = foo_impl<>;
int main() { return foo{}.x.value; }
Run Code Online (Sandbox Code Playgroud)
这适用于所有主要编译器.我有三个问题:
template
)被认为是无效的?如果编译器可以找出第二个选项,我没有看到为什么它无法找出第一个选项的原因.如果我添加一个明确的专业化void
:
template <typename = void>
struct …
Run Code Online (Sandbox Code Playgroud) 我可以通过这样做来转发在命名空间中声明一个函数:
void myNamespace::doThing();
Run Code Online (Sandbox Code Playgroud)
这相当于:
namespace myNamespace
{
void doThing();
}
Run Code Online (Sandbox Code Playgroud)
要在名称空间中转发声明类:
namespace myNamespace
{
class myClass;
}
Run Code Online (Sandbox Code Playgroud)
有没有更短的方法来做到这一点?我正在思考以下问题:
class myNamespace::myClass;
Run Code Online (Sandbox Code Playgroud) 编译此.c源文件时出现此错误
/INIT_SOURCE_BUILD/src/names_list.c:7:错误:"名"的存储大小是未知
#include <stdio.h>
#include "list.h"
int main(){
struct List names;
names->size = 3;
struct ListElmt michael;
struct ListElmt john;
struct ListElmt adams;
names->head = michael;
michael->data = 12;
michael->next = john;
john->data = 14;
john->next = adams;
adams->data = 16;
struct ListElmt pointer = List->head;
for(int x = 0; x < 3 ; x++){
printf("Iteration.%d data: %d", x, pointer->data);
pointer->next = pointer->next->next;
}
}
Run Code Online (Sandbox Code Playgroud)
这是这个链表的标题
#ifndef LIST_H
#define LIST_H
#include <stdio.h>
/* Define linked list elements*/
typedef struct _ListElmt{ …
Run Code Online (Sandbox Code Playgroud) // This is a header file.
class MyClass; // It can be forward declared because the function uses reference.
// However, how can I do forward declaraion about std::wstring?
// class std::wstring; doesn't work.
VOID Boo(const MyClass& c);
VOID Foo(const std::wstring& s);
Run Code Online (Sandbox Code Playgroud) 如何调用需要从其创建之上调用的函数?我读了一些关于前向声明的内容,但谷歌在这种情况下并没有帮助.这个的正确语法是什么?
我只是不明白为什么枚举的大小与编译器相关,而类的大小不是.
我的代码示例:
class A;
enum E; // must be enum E : int; in order to compile
void f(const A & param);
void f(const E & param);
Run Code Online (Sandbox Code Playgroud)
我在这里谈论标准C++编译器.我知道MSVC让它编译并正常工作.所以问题是:
为什么这还没有标准化?
考虑以下两种情况(编辑只是为了完成整个问题并使其更清晰)
案例1 :(如下面正确提到的那样编译)
//B.h
#ifndef B_H
#define B_H
#include "B.h"
class A;
class B {
A obj;
public:
void printA_thruB();
};
#endif
//B.cpp
#include "B.h"
#include <iostream>
void B::printA_thruB(){
obj.printA();
}
//A.h;
#ifndef A_H
#define A_H
#include "A.h"
class A {
int a;
public:
A();
void printA();
};
#endif
//A.cpp
#include "A.h"
#include <iostream>
A::A(){
a=10;
}
void A::printA()
{
std::cout<<"A:"<<a<<std::endl;
}
//main.cpp
#include "B.h"
#include<iostream>
using namespace std;
int main()
{
B obj;
obj.printA_thruB();
}
Run Code Online (Sandbox Code Playgroud)
案例2 :(唯一的修改......没有编译错误)
//B.h
#include …
Run Code Online (Sandbox Code Playgroud)