此函数是全局的,并在头文件中定义(暂时我想将其保留在那里).
头文件也构成一个具有内联函数的特定类,其中一个函数调用此全局函数.
源文件不包含任何有问题的全局函数.
有关错误原因的任何提示?
如果有人有兴趣,我可以发布代码.
mainwindow.o: In function `tileForCoordinate(double, double, int)':
mainwindow.cpp:(.text+0x310): multiple definition of `tileForCoordinate(double, double, int)'
main.o:main.cpp:(.text+0xd0): first defined here
moc_mainwindow.o: In function `qHash(QPoint const&)':
moc_mainwindow.cpp:(.text+0x0): multiple definition of `qHash(QPoint const&)'
main.o:main.cpp:(.text+0x0): first defined here
moc_mainwindow.o: In function `tileForCoordinate(double, double, int)':
moc_mainwindow.cpp:(.text+0x150): multiple definition of `tileForCoordinate(double, double, int)'
main.o:main.cpp:(.text+0xd0): first defined here
collect2: ld returned 1 exit status
make: *** [SimpleRouting] Error 1
Run Code Online (Sandbox Code Playgroud) 我刚刚写了一个包含一些静态数据成员的类,但现在我收到有关"未定义引用"的错误.为什么这不起作用?我究竟做错了什么?
(注意:这是Stack Overflow的C++常见问题解答的一个条目.如果你想批评在这种形式下提供常见问题解答的想法,那么发布所有这些的元数据的发布将是这样做的地方.这个问题在C++聊天室中受到监控,其中FAQ的想法一开始就出现了,所以你的答案很可能被那些提出想法的人阅读.)
为什么匿名命名空间中定义的符号(函数和变量)与static关键字没有内部链接?如果某个功能在外面不可见/可访问,那么外部链接的原因是什么?
我有一个名为"SimpleFunctions.h"的文件定义如下:
#ifndef SIMPLEFUNCTIONS_H
#define SIMPLEFUNCTIONS_H
namespace my_namespace {
double round(double r) { return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5); }
float round(float r) { return round((double)r); }
}
#endif // SIMPLEFUNCTIONS_H
Run Code Online (Sandbox Code Playgroud)
此文件以前只包含在一个文件中,并且工作正常.
今天我把它包含在第二个文件中,它不再有效.在链接时,它告诉我该函数已在"firstfile.obj"中定义.
但是,由于我使用包含警卫,我希望这些函数只定义一次,或者我错过了什么?
我有两个C文件.
在file1.c
int main()
{
func();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
file2.c中
static void func(void)
{
puts("func called");
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我用命令编译上面的代码cc file2.c file1.c
,我得到了以下内容,
undefined reference to `func'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
但是,如果我删除file2.c中的static
关键字并使用命令编译上面的代码,它就会成功运行.cc file2.c file1.c
所以,我有一个问题,C中的void和static void函数有什么区别?
我正在写一个Cocos2D-X游戏,其中玩家,敌人和其他角色将他们的属性存储在a中CCMutableDictionary
,这有点像装饰类std::map<std::string, CCObject*>
.可以通过该CCMutableDictionary::objectForKey(const std::string& key)
方法访问字典中的值.
现在,在我的许多.cpp文件包含的头文件中,我有一些const char * const
字符串用于访问字典中的值,如下所示:
// in Constants.h
const char* const kAttributeX = "x";
const char* const kAttributeY = "y";
// in a .cpp file
CCObject* x = someDictionary->objectForKey(kAttributeX);
Run Code Online (Sandbox Code Playgroud)
所以,如果我错了,请纠正我,但每当我使用a 调用上述方法之一时,std::string
正在调用复制构造函数并且临时std::string
存在于堆栈中,对吧?objectForKey
const char* const
如果是这样,我觉得如果那些常量属性键已经是std::string
对象,那么在运行时它会更有效.但是我该如何以正确的方式做到这一点?
在Constants.h文件中定义它们,如下所示编译好,但我觉得有些事情是不对的:
// in Constants.h
const std::string kAttributeX = "x";
const std::string kAttributeY = "y";
Run Code Online (Sandbox Code Playgroud)
如果已经提出这个问题我很抱歉.我似乎找不到我在StackOverflow上寻找的确切答案.
我不知道为什么这会让我疯狂,但确实如此.我在main中定义了一个函数并且声明了forward.
static void myFunc(int x);
static void myFunc( int x)
{
//do stuff
}
main()
Run Code Online (Sandbox Code Playgroud)
我想在另一个类中使用myFunc(int x).所以我认为我所要做的就是在该类头中使用extern static void myFunc(int x),然后在类定义中调用它我需要的地方,但它不起作用.
我究竟做错了什么?
谢谢
我有2个文件A.cpp和B.cpp看起来像
A.cpp
----------
class w
{
public:
w();
};
B.cpp
-----------
class w
{
public:
w();
};
Run Code Online (Sandbox Code Playgroud)
现在我读到了某个地方(http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr082.htm)连锁.因此,虽然构建我期待多重定义错误,但相反它有点像魅力.但是当我在A.cpp中定义类w时,我得到了重定义错误,这让我相信类具有内部链接.
我在这里错过了什么吗?
在我的一个类中,我试图使用std::priority queue
指定的lambda进行比较:
#pragma once
#include <queue>
#include <vector>
auto compare = [] (const int &a, const int &b) { return a > b; };
class foo
{
public:
foo() { };
~foo() { };
int bar();
private:
std::priority_queue< int, std::vector<int>, decltype(compare)> pq;
};
Run Code Online (Sandbox Code Playgroud)
我的程序编译完美,直到我添加一个.cpp
文件随附标题:
#include "foo.h"
int foo::bar()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这次,我的编译器生成错误:
>main.obj : error LNK2005: "class <lambda> compare" (?compare@@3V<lambda>@@A) already defined in foo.obj
Run Code Online (Sandbox Code Playgroud)
.cpp
如果我的头文件包含lambda,为什么我不能创建一个附带的文件?
编译器:Visual Studio 2012
我的main.cpp
:
#include "foo.h"
int …
Run Code Online (Sandbox Code Playgroud) 在C++ 1z中声明内存有效全局常量的最佳方法是什么,它不进行内部链接,因此在所有翻译单元中使用单个副本?
尽管在许多地方已经提到过,但我们没有任何单一的"最佳方法"问题和答案,所以在这里.以下是我找到相关问题的部分地方列表.
我在找为atleast所有基本基本类型的解决方案,如int
,double
,char
,std::string
.
编辑1: 最好的方式,我的意思是"记忆效率".我知道对于内部联系,每个翻译单元都会有多个副本,因此会占用内存.除此之外,如果我们能够实现快速的执行时间和编译时间,这将是伟大的,代码的美化(容易)并不重要.此外,我知道外部链接将增加获取时间,因为处理器可能会在运行时获得缓存未命中.但最新的C++语言是否支持所有这些的最佳方法?或者可能建议支持每个案例?
注意:对于std::string
全局常量,最佳方法是什么?是否存在可变性会对有任何影响常量性?