namespace {int Foo(int a); }
像这样.这段代码是否合法?
这合法吗?而且,我可以在任何地方引用Foo吗?或者只是某个域名?
谢谢.
我正在用C++构建一个演化模拟器,但不是一个"真正的"可运行程序,而是一个其他程序应该的类#include.这个类,叫World,有一些功能,如update(),getInfo()等...
我在这里遇到两个问题.首先,我不知道我应该如何编译该类,以及我应该为哪些文件提供用户程序(#include该类的用户程序).显然,程序应该接收.hpp文件,但还有什么?该类的目标文件World?这意味着我需要用g++ World.o user.o -o user语法编译用户程序,但有没有办法避免这样做(World.o在我的编译命令中提到)?我不需要iostream.o在编译命令中包含相同的方法.
第二个问题是World该类#include是一些其他类,例如Organism,它们必须包含Block该类才能从中继承.如何编译此代码以获取单个World.o文件(如果这是问题1的答案)?
我一直在尝试使用extern来使用先前定义的变量.
我以前没有使用extern,现在我需要使用它来定义变量一次并在多个文件中使用它们
我已经为这个问题编写了最小化代码版本.我有四个文件
lib.h
#ifndef LIB_H
#define LIB_H
#include <iostream>
namespace lib {
extern bool initialized;
bool initialized = false;
static void isInit(char* parent) {
std::cout << "Library for [" << parent << "] initialized? " << (::lib::initialized ? "yes" : "no") << "\n";
}
} // namespace lib
#endif
Run Code Online (Sandbox Code Playgroud)
vehicle.h
#ifndef _VEHICLE_H
#define _VEHICLE_H
#include <string>
class Vehicle {
public:
Vehicle(const std::string& manufacturer,
const std::string& model,
int year);
std::string manufacturer;
std::string model;
int year;
};
#endif
Run Code Online (Sandbox Code Playgroud)
以下是vehicle.h文件的实现,名为vehicle.cpp
#include "vehicle.h" …Run Code Online (Sandbox Code Playgroud) 新EE在这里只有很少的软件经验.在过去的几年里,在这个网站上看过很多问题,这将是我的第一个问题/帖子.还没有找到这个答案.
我想知道函数修改体内的全局变量(不将其作为参数传递),以及传递变量的地址之间的差异/动机.
以下是每个更清晰的示例.假设我正在声明一些函数"peripheral.c"(在"peripheral.h"中使用它们的原型,并在"implementation.c"中使用它们).
方法1:
//peripheral.c
//macros, includes, etc
void function(*x){
//modify x
}
Run Code Online (Sandbox Code Playgroud)
.
//implementation.c
#include "peripheral.h"
static uint8 var;
function(&var); //this will end up modifying var
Run Code Online (Sandbox Code Playgroud)
方法2:
//peripheral.c
//macros, includes, etc
void function(void){
//modify x
}
Run Code Online (Sandbox Code Playgroud)
.
//implementation.c
#include "peripheral.h"
static uint8 x;
function(); //this will modify x
Run Code Online (Sandbox Code Playgroud)
是避免使用"全局"变量的唯一动机吗?(另外,如果它只有文件范围,它真的是全局的吗?)
希望这个问题有道理.谢谢
如果我用静态声明全局变量,我有一个疑问.
static int a=5;
main()
{
func();
}
Run Code Online (Sandbox Code Playgroud)
可以使用extern在另一个file2.c中访问吗?
func()
{
extern int a;
printf(a);
}
Run Code Online (Sandbox Code Playgroud)
或者只使用extern来声明没有静态声明的全局变量?
假设代码:
extern int foo(void);
static int foo(void)
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
尝试使用GCC进行编译
$ gcc -Wall -std=c99 1.c
1.c:3:12: error: static declaration of ‘foo’ follows non-static declaration
1.c:1:12: note: previous declaration of ‘foo’ was here
1.c:3:12: warning: ‘foo’ defined but not used [-Wunused-function]
Run Code Online (Sandbox Code Playgroud)
那么,我该如何声明静态函数呢?
在标准中它说:
当名称具有内部链接时,其表示的实体可以通过同一翻译单元中其他范围的名称来引用.
和:
具有命名空间作用域(3.3.6)的名称具有内部链接(如果它是 - 一个显式声明为静态的变量,函数或函数模板的名称);
请考虑以下代码:
#include <stdio.h>
namespace A
{
/* a with internal linkage now.
Entity denoted by a will be referenced from another scope.
This will be main() function scope in my case
*/
static int a=5;
}
int main()
{
int a; //declaring a for unqualified name lookup rules
printf("%d\n",a);//-1216872448
}
Run Code Online (Sandbox Code Playgroud)
我真的不明白标准中的定义.这意味着什么:
它表示的实体可以通过同一翻译单元中其他范围的名称来引用.
声明:"必须在调用内联函数之前定义它们."
这个陈述是否正确?
[编辑]
问题最初是德语:
Inline-Funktionenmüssenvorihrem Aufruf definiert sein.
也许它可以帮助任何人......
我被告知,无法从其他文件访问static在一个.c文件中定义的函数.但是在下面的程序中,我可以static void show()从另一个文件访问该函数.我对staticC 中函数的理解是错误的吗?
啊(第一档):
static void show()
{
printf("I am in static show function in a.c");
}
Run Code Online (Sandbox Code Playgroud)
bc(另一个文件):
#include"a.h"
void main()
{
show();
}
Run Code Online (Sandbox Code Playgroud) 我以前曾多次使用过包括警卫,但从未真正理解他们的工作方式或原因.
以下为什么不工作?
#ifndef CAMERA_CLASS_HPP
#define CAMERA_CLASS_HPP
class camera_class
{
....
};
camera_class glcam = camera_class();
#endif // CAMERA_CLASS_HPP
Run Code Online (Sandbox Code Playgroud)
错误是这样的:(你可以从这个问题的标题中猜出它会是什么!)
-------------- Build: Debug in System ---------------
Linking console executable: bin/Debug/System
/usr/bin/ld: error: obj/Debug/main.o: multiple definition of 'glcam'
/usr/bin/ld: obj/Debug/camera_class.o: previous definition here
/usr/bin/ld: error: obj/Debug/main.glfunc.o: multiple definition of 'glcam'
/usr/bin/ld: obj/Debug/camera_class.o: previous definition here
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 0 warnings
Run Code Online (Sandbox Code Playgroud)
另外,有人可以向我解释为什么头部防守工作吗?