是否有任何常用的C++编译器允许这种语法
Are there any commonly used C++ compilers that would allow this syntax?
我是C++新手,并试图让这个开源程序(为/在 Linux 中开发(在 OS X 上的 xcode 中编译和运行。
当我编译并运行代码时,我收到很多错误(超过 xcode 愿意计算(,例如use of undeclared identifier 'x'
或use of undeclared identifier 'y'
下面是引发错误的代码示例:
template<typename T>
struct TVector2 {
T x, y;
TVector2(T _x = 0.0, T _y = 0.0)
: x(_x), y(_y)
{}
double Length() const {
return sqrt(static_cast<double>(x*x + y*y));
}
double Norm();
TVector2<T>& operator*=(T f) {
x *= f;
y *= f;
return *this;
}
TVector2<T>& operator+=(const TVector2<T>& v) {
x += v.x;
y += v.y;
return *this;
}
TVector2<T>& operator-=(const TVector2<T>& v) {
x -= v.x;
y -= v.y;
return *this;
}
};
struct TVector3 : public TVector2<T> {
T z;
TVector3(T _x = 0.0, T _y = 0.0, T _z = 0.0)
: TVector2<T>(_x, _y), z(_z)
{}
double Length() const {
return sqrt(static_cast<double>(x*x + y*y + z*z)); //use of undeclared identifier x
}
double Norm();
TVector3<T>& operator*=(T f) {
x *= f;
y *= f;
z *= f;
return *this;
}
在我看来,作为一个没有经验的C++程序员,看起来x和y只是未声明的局部变量。我可以通过简单地声明变量来让编译器摆脱错误,就像这样......
struct TVector3 : public TVector2<T> {
T z;
T x;
T y;
然而,这些错误的绝对数量让我认为
- 可能有(相当常见的(C++编译器版本允许您将变量 x 声明为 _x。这可以解释为什么我下载的源代码有这么多编译器错误。
- 也许我得到了一个"坏批次"的源代码,我不应该浪费时间让它编译,因为源代码以某种方式很糟糕。
有经验的C++开发人员可以解释一下可能发生的事情吗?
-
x
和y
是基类TVector2<T>
的数据成员。 -
因为基类是依赖于模板参数
T
的类型,所以在查找非限定名时不会搜索基类。 -
我相信 MSVC 曾经编译过这段代码,不确定它是否仍然在 C++11 模式下。原因是 MSVC 没有在模板中正确执行名称解析。
-
解决方法通常是说
this->x
而不是x
。
相关文章:
- C/C++编译器通常会删除重复的库吗
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- Win32编译器选项和内存分配
- MSVC多行宏编译器错误
- 在使用包含冒号的类似函数的宏时,是什么导致了这种编译器差异?
- 为什么我的编译器无法弄清楚这种转换,它何时存在?
- 在这种情况下,编译器会进行优化吗?
- C++ 编译器正在更改我的结构的对齐方式.我怎样才能防止这种情况
- 为什么c++编译器接受这种初始化?静态int x=x
- C++ 编译器是否将这种形式的"new"视为堆栈内存?
- 为什么C++编译器在这种特殊情况下需要 "typename" 关键字?
- 这种编译器优化不一致是否完全由未定义的行为来解释
- 编译器在这种情况下会做什么
- 为什么编译器在这种情况下没有省略副本构造
- 是否有任何常用的C++编译器允许这种语法
- 为什么编译器在这种情况下选择了不正确的函数重载
- 在这种情况下,编译器可以根据自己的意愿对指令进行重新排序
- 为什么这种明显的无限递归没有给出编译器警告?
- 这种情况的结果会是什么?它是定义的还是与编译器相关的
- 编译器如何隐式地进行这种转换