dir1有dir2、file1.c和file1.h。
dir2 有 file2.c
现在,如果我想在 file2.c 中访问 file1.c 中定义的函数,我需要在 file1.h 中声明它并在 file2.c 中包含 file1.h ——这是一个有效的假设吗?
如果不是,请解释一下。
如果是,即使这样做之后我也会收到“未定义的函数引用”错误。
file2.c:29: 对“函数”collect2 的未定义引用:ld 返回 1 退出状态 *错误代码 1
在 C 中,我有一个头文件,仅由和file1_0.h包含。file1.hfile1.c
file1.h被许多其他文件包含,例如file2.c,file3.c和file4.c
如果我进行任何更改,file1_0.h我知道我需要重新编译file1.c,但是我还需要重新编译file2.c,file3.c和 吗file4.c?
我是 C 新手,似乎对头文件的工作方式有误解。为简单起见,我有三个文件:tree.h、lib.c 和 main.c
在 tree.h 中,我有
struct Node
{
void* item;
Node** nodes;
};
struct Tree
{
Node* tree_root;
int depth, item_size;
};
void initializeTree(Tree*, int);
Run Code Online (Sandbox Code Playgroud)
我的理解是,这个 initializeTree 方法是一个函数“签名”,并且每当我在 lib.c 或任何其他包含头文件 tree.h 的 .c 文件中调用它时,编译器都会知道一些关于该函数的信息. 但是,在 lib.c 中我有错误“标识符树未定义”。
#include <tree.h>
void initializeTree(Tree* tree, int item_size)
Run Code Online (Sandbox Code Playgroud)
是什么导致了这个错误?编译器是否无法从包含的头文件中“看到”树结构?
有两个文件,一个是.cpp文件,另一个是.h文件,头文件
该类存在于.h文件中
class DecodeTheCode
{
public:
char* decodeCode(char* encodedString);
};
Run Code Online (Sandbox Code Playgroud)
现在我正在使用的软件问
•在函数char*decodeCode(char*encodedString)中实现逻辑
但我已宣布
const char* decodeCode(const char* encodedString)
{
const char* decodedString = "";
const char* a = encodedString;
char store[10000];
for(int j=0;j<strlen(a);j++)
{
if (isdigit(a[j]) || a[j] == '#')
continue;
else return "";
}
int i = 0,k=0;
while (i < strlen(a))
{
if (a[i] == '#') {i++; continue;}
else if(a[i] == a[i+1] && a[i+1] == a[i+2] && a[i+2] == a[i+3])
{
store[k++] = four(a[i]);
i += 4;
} …Run Code Online (Sandbox Code Playgroud) #import我的标题和实现在哪里?
我应该做什么规则?
有没有办法在单个头文件中导入所有内容?
我正在尝试创建一个新的Node cpp/h文件,但是我收到以下错误:
字段'指针'具有不完整类型'节点'
头文件
#ifndef NODE_HEADER
#define NODE_HEADER
template <class T>
class Node {
private:
T value;
Node<T> pointer; //The problem seems to be here
public:
Node<T>();
Node<T>(T value);
Node<T>(T value, Node<T> pointer);
T get_value();
void set_value(T value);
Node<T> get_pointer();
void set_pointer(Node<T> pointer);
};
#endif
Run Code Online (Sandbox Code Playgroud) 在 Macbook 上,我正在用 json-c( https://github.com/json-c/json-c )做一些事情
gcc a.c
a.c:1:10: fatal error: 'json.h' file not found
#include "json.h"
^
1 error generated.`>
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,
它打印出错误,但我json.h在包含文件中有文件。
> cd /usr/local/include/json-c/
> ls
arraylist.h json_config.h json_tokener.h
bits.h json_inttypes.h json_util.h
debug.h json_object.h linkhash.h
json.h json_object_iterator.h printbuf.h
json_c_version.h json_object_private.h random_seed.h
Run Code Online (Sandbox Code Playgroud)
/usr/include/json-c/json.h 肯定存在
我有两个常量文件:
// constants.h
extern const std::string testString;
// constants.cpp
const std::string testString = "defined!";
Run Code Online (Sandbox Code Playgroud)
我需要在程序初始化时在对象的构造函数中使用此常量,但它是未定义的.构造函数中的代码是:
MyClass::MyClass() {
printf("Value of test string: %s", testString.c_str());
}
// output:
Value of test string: (null)
Run Code Online (Sandbox Code Playgroud)
类和常量在同一名称空间中定义,并且它不会给我一个未定义常量的错误.初始化对象后(例如,使用硬编码字符串),它可以正常工作并打印出常量的值("已定义!").原始常量似乎在构造函数中工作正常.
我认为它与当时未初始化的常量有关(所以从.cpp文件开始).你知道为什么会这样吗?在程序完全初始化之后,是否会发生extern const的初始化?
先感谢您
编辑:
请注意,string类型是为了简化问题,因此将其转换char为不是一个选项,因为我也对其他非基本类型感兴趣.
显示此问题的程序的最小脏示例的代码:
// constants.h
extern const std::string testString;
// constants.cpp
#include "constants.h"
const std::string testString = "defined!";
// MyClass.h
class MyClass {
public:
MyClass();
virtual ~MyClass();
};
// MyClass.cpp
#include "MyClass.h"
#include "constants.h"
MyClass::MyClass() {
printf("Value of test string: %s\n", …Run Code Online (Sandbox Code Playgroud) 我正在查看queue头文件C++并找到了一段代码。
来自队列头文件的一段代码
#include <debug/debug.h>
#include <bits/move.h>
#include <bits/predefined_ops.h>
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
/**
* @defgroup heap_algorithms Heap
* @ingroup sorting_algorithms
*/
template<typename _RandomAccessIterator, typename _Distance,
typename _Compare>
_GLIBCXX20_CONSTEXPR
_Distance
__is_heap_until(_RandomAccessIterator __first, _Distance __n,
_Compare& __comp)
{
_Distance __parent = 0;
for (_Distance __child = 1; __child < __n; ++__child)
{
if (__comp(__first + __parent, __first + __child))
return __child;
if ((__child & 1) == 0)
++__parent;
}
return __n;
}
Run Code Online (Sandbox Code Playgroud)
@它是什么以及为什么在多行注释中使用它。后面的文本@也在我的 IDE …
header-files ×10
c ×5
c++ ×4
build-time ×1
comments ×1
dependencies ×1
gcc ×1
json ×1
json-c ×1
objective-c ×1
operators ×1
recompile ×1
xcode ×1