对于C++中的命名空间,我有一个简单的问题。当我编译下面的小代码时会出现错误。我不明白为什么。谢谢您提前提供帮助!
#include <iostream>
using namespace std;
int x=100;
namespace first
{
int x=1;
}
namespace second
{
int x=2;
}
int main(){
{
using namespace first;
cout<<x<<endl;
}
{
usi
我正在使用OpenGL在c++中做一个项目,不断收到来自cmath.h的数以百计的语法错误。主要的难题是,我甚至根本没有引用或包括cmath。我在一两件事中使用了math.h,但在我最近进行更改之前,这段代码工作得很好。返回到以前的工作代码,现在显示了相同的错误,所以我有点困惑于从哪里开始查找。
我是不是漏掉了什么很明显的东西,或者这可能是Visual Studio 10的一个问题?
我试图理解为什么这个程序没有给我一个名称查找的模糊性:
namespace X { int i = 1; }
namespace Q {
namespace P {
int i = 2;
using namespace X;
}
using namespace P;
int l = i;
}
int main() {}
如果我们像这样修改它,我们就会得到一个名称查找歧义:
namespace X { int i = 1; }
namespace P {
int i = 2;
在编译我的程序之后
$ cmake .
$ make
我收到这份表格的5处错误:
$ make
Scanning dependencies of target Project
[ 50%] Building CXX object CMakeFiles/myproj.dir/main.cpp.o
In file included from /path/to/my/proj/main.cpp:2:0:
/path/to/my/proj/library.hpp: In member function ‘virtual double MyProjClass::get_value(double, do
所以我正在看各种问题来准备明天的面试,我遇到了extern关键字,我知道extern关键字指定为另一个程序的一部分(不知道在哪里使用)的变量分配内存,但真正的疑问是,
#include<iostream>
using std::cout;
int main()
{
extern int a;
cout<<a;
return 0;
}
int a=20;
output:
20
我想知道这是怎么回事?即使提升在c++中工作,我知道一些JS提升,就像即使先声明一个为int a,然后再完成赋值,输出应该是一个值一个垃圾值……
我正在寻找一种官方的(或至少半官方的)方式来暴露我的操作员在我的标准类库。
例如,我实现了一个,这样就可以在编译时编写128个位整数。到目前为止,当我实现运算符时,我只会将它们放在我的命名空间之外,以使它们都在没有using namespace snapdev的情况下工作--这很糟糕,因为这会带来名称空间中的所有内容,我当然不想这样做。
对于文字,我已经看到您可以使用namespace literals子名称空间,就像在中一样。
现在,我有许多类,它们有一个与我的ostream实现类似的相应的运算符:
template<class E, class S, class T, class S
有样品
// Example 2: Will this compile?
//
// In some library header:
namespace N { class C {}; }
int operator+(int i, N::C) { return i+1; }
// A mainline to exercise it:
#include <numeric>
int main()
{
N::C a[10];
std::accumulate(a, a+10, 0);
}
来自“例外C++:47个工程难题,编程问题和解决方案”名称查找和接口原理-第4部分
g++
我不明白为什么我会犯这个“奇怪的”错误。我读了类似的问题,但它没有回答我的问题。如果我在main函数而不是全局作用域中定义数组,就不会出现错误。但是假设我必须在全局范围内定义这个数组。为什么我要接受这个错误?代码如下:
#include <iostream>
#include <cstring>
using namespace std;
int right[1005];
int main()
{
memset(right,0,sizeof(right));
return 0;
}
下面是错误:
memset2.cpp: In function ‘int ma
有没有人能给我解释一下:
$ cat test.cpp
#include <string>
std::string div;
$ g++ -c test.cpp
$ g++ -std=c++11 -c test.cpp
test.cpp:2:13: error: 'std::string div' redeclared as different kind of symbol
In file included from /usr/include/c++/4.7.1/cstdlib:66:0,
from /usr/include/
首先,我对c++很陌生,并且“尝试”给我的变量加上前缀。但我不太清楚。所以我的问题是,用"g_“作为静态变量的前缀正确吗?谢谢!
using namespace std;
// The main window class name.
static TCHAR g_szWindowClass[] = _T("win32app");
// The string that appears in the application's title bar.
static TCHAR g_szTitle[] = _T("Win32 App");
...
我需要在主函数中设置一个变量,并从同一个文件中的不同函数中访问它。我不能将它传递给函数,因为它意味着更改整个代码结构,这不是一个选项。为了避免声明全局变量,我使用了一个命名空间,并且我想检查这是一个好的编程实践,还是有一个更干净的方法来实现它。这是代码:
namespace mylocalnamespace{
int myglobalvar;
}
static void myFunc()
{
..... some code
operationX(mylocalnamespace::myglobalvar);
..... some code
}
int mai
我正在尝试在Linux上编译上的文件。
当我运行./configure时,一切都很顺利,直到我输入make。然后我得到以下错误:
In file included from ./string.h:33,
from HtmlTokenizer.cpp:24:
/usr/include/c++/4.4/cstring:76: error: ‘::memchr’ has not been declared
/usr/include/c++/4.4/cstring:77: error: ‘::memcmp’ has not been declared
/usr/inc
这是对另一个的后续。
最初的问题还有其他问题,但我不得不意识到,主要的问题(根据CLang)是将time重新定义为一个不同的符号,而使用的只有漂亮的C++。
下面是一个精简的版本:
#include<iostream>
using std::cout;
using std::endl;
class time
{
public:
int h, min, sec;
};
const int full = 60;
void canonify(time& pre) // Error here (line 14)
{
pre.min += pre.se
在遇到许多关于为什么不使用像std这样的namespace的问题和阅读文档后,我有了这个想法,这样我就不必每次都输入std::。
namespace leanandmean
{
using std::cout;
using std::endl;
using std::map;
}
using namespace leanandmean;
因此,我将只使用库和名称空间中需要的方法。我一直在想它可能导致的错误,所以最后一个问题是,使用这种方法是否完全可以?
算法头文件中不是已经有一个max函数了吗?通过using namespace std;,我将函数导入到全局命名空间(这需要参数,在本例中这两个函数都是整数,所以不应该是重载)。
那么为什么没有命名冲突呢?
#include <iostream>
#include <algorithm>
using namespace std;
int max(int a, int b)
{
return (a > b) ? a : b;
}
int main()
{
cout << max(5, 10) << endl;
}