C++和C中的翻译单元之间有区别吗?
在其他帖子中,我读到标题和源文件构成了一个翻译单元,但是单独的源文件可以在C++中被称为翻译单元,其中它包含一个文件中的所有定义吗?
有人可以解释这个C++编译错误的本质吗?我正在涉及/学习重载全局运算符new,delete及其变体.我读了一对夫妇 的 文章 上 的 主题,但我无法找到一个似乎专门解决这个问题.
代码
foo.h:
#ifndef foo_h
#define foo_h
void* operator new(size_t);
void* operator new[](size_t);
void operator delete(void*);
void operator delete[](void*);
#endif // foo_h
Run Code Online (Sandbox Code Playgroud)
foo.cpp:
#include <foo.h>
#include <iostream>
void* operator new(size_t size) { return NULL; }
void* operator new[](size_t size) { return NULL; }
void operator delete(void* p) { }
void operator delete[](void* p) { }
Run Code Online (Sandbox Code Playgroud)
编译错误
>g++ -g -std=c++14 -I./ -c foo.cpp -o foo.o
In file included from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/ext/new_allocator.h:33:0,
from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/x86_64-pc-cygwin/bits/c++allocator.h:33, …Run Code Online (Sandbox Code Playgroud) 在C++ 1z中声明内存有效全局常量的最佳方法是什么,它不进行内部链接,因此在所有翻译单元中使用单个副本?
尽管在许多地方已经提到过,但我们没有任何单一的"最佳方法"问题和答案,所以在这里.以下是我找到相关问题的部分地方列表.
我在找为atleast所有基本基本类型的解决方案,如int,double,char,std::string.
编辑1: 最好的方式,我的意思是"记忆效率".我知道对于内部联系,每个翻译单元都会有多个副本,因此会占用内存.除此之外,如果我们能够实现快速的执行时间和编译时间,这将是伟大的,代码的美化(容易)并不重要.此外,我知道外部链接将增加获取时间,因为处理器可能会在运行时获得缓存未命中.但最新的C++语言是否支持所有这些的最佳方法?或者可能建议支持每个案例?
注意:对于std::string全局常量,最佳方法是什么?是否存在可变性会对有任何影响常量性?
我是c/c ++的新手,我对以下内容感到困惑:
<iostream>在example.h文件或example.cpp文件中放置标题?<iostream>,并且我将一个类的头文件包含在另一个类的头中,这是否意味着我包含了<iostream>两次?std::?my_test.h
#ifndef MY_TEST
#define MY_TEST
struct obj {
int x;
int y;
};
class A {
private:
const static int a=100;
const static obj b;
};
const obj A::b={1,2};
#endif
Run Code Online (Sandbox Code Playgroud)
使用此头文件编译cpp时,'multiple definition of 'A::b'会发生错误.
A::a生产错误?(我不能写代码const static obj b={1,2}中class A)举个例子:
// myheader.h
static int myStaticVar = 0;
// If we remove 'static' the compiler will throw linker error.
void DoStuff();
// and myheader.cpp, and main.cpp; etc
Run Code Online (Sandbox Code Playgroud)
这就是我解释它的方式:
静态变量没有外部链接,当我们编译没有'static'时,我们在每个文件中"包含"静态变量(这里是全局的),这会创建重复项,并且链接器将抛出错误,因为不允许多次声明.
有没有更好的方法来解释这个?谢谢.
PS:我们假设在头文件中有静态变量(不是在谈论成员)吗?
我正在使用QT 5.5.0.
当我编译一个程序时,它显示"命名空间'std'中没有名为'u16string'的类型".有趣的是,我过去成功地编译了它,为什么它现在失败了?这似乎很麻烦qstring.h.
我如何解决它?这是错误发生的地方
#ifndef QSTRING_H
#define QSTRING_H
#if defined(QT_NO_CAST_FROM_ASCII) && defined(QT_RESTRICTED_CAST_FROM_ASCII)
#error QT_NO_CAST_FROM_ASCII and QT_RESTRICTED_CAST_FROM_ASCII must not be defined at the same time
#endif
#include <QtCore/qchar.h>
#include <QtCore/qbytearray.h>
#include <QtCore/qrefcount.h>
#include <QtCore/qnamespace.h>
#include <string>
#if defined(Q_OS_ANDROID)
// std::wstring is disabled on android's glibc, as bionic lacks certain features
// that libstdc++ checks for (like mbcslen). namespace std { typedef basic_string<wchar_t> wstring; }
#endif
#if defined(Q_COMPILER_UNICODE_STRINGS) || defined(Q_QDOC)
static inline QString fromStdU16String(const std::u16string &s);
inline std::u16string toStdU16String() const; …Run Code Online (Sandbox Code Playgroud) namespace {int Foo(int a); }
像这样.这段代码是否合法?
这合法吗?而且,我可以在任何地方引用Foo吗?或者只是某个域名?
谢谢.
这段代码有什么问题:
标题:
#include <map>
using namespace std;
template<class T>
class ValueCollection
{
public:
ValueCollection(void);
int getValueCount(void);
map<string, T> Values;
};
Run Code Online (Sandbox Code Playgroud)
执行:
#include "ValueCollection.h"
ValueCollection<class T>::ValueCollection(void)
{
}
int ValueCollection<class T>::getValueCount(void)
{
return Values.size();
}
Run Code Online (Sandbox Code Playgroud)
测试:
#include "ValueCollection.h"
TEST(ValueCollection_TestCases, Default_Constructor_MapIsEmpty)
{
ValueCollection<int>* target = new ValueCollection<int>;
int expected = 0;
int actual = target->getValueCount();
ASSERT_EQ(expected, actual);
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
Error 1 error C2079: 'std::_Pair_base<_Ty1,_Ty2>::second' uses undefined class 'T' c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility 167 1 Refactor01
Run Code Online (Sandbox Code Playgroud)