矩阵类:错误:")"标记之前的预期主表达式
Matrix Class: error: expected primary-expression before ')' token
我有一个Matrix4类派生/扩展矩阵基类(模板类)。模板类的方法在同一个头文件中声明和定义。
我只复制了给出错误的"Matrix4"类的部分。同样的错误出现在第10行&13. 我看不到任何缺失的变量或参数。我试过去掉括号,但是没有用。
我已经搜索了一个线索,我可能做错了什么,但我没有发现任何有用的类似问题在这个网站上…我真的很感激你的帮助。
给出错误的Matrix4类:template<typename T>
class Matrix4 : public Matrix<T, 4>
{
public:
Matrix4() { }
inline Matrix4 InitOrthographic(T left, T right, T bottom, T top, T near, T far)
{
const T width = (right - left);
const T height = (top - bottom);
const T depth = (far - near); //error occurs here
(*this)[0][0] = T(2)/width; (*this)[1][0] = T(0); (*this)[2][0] = T(0); (*this)[3][0] = -(right + left)/width;
(*this)[0][1] = T(0); (*this)[1][1] = T(2)/height; (*this)[2][1] = T(0); (*this)[3][1] = -(top + bottom)/height;
(*this)[0][2] = T(0); (*this)[1][2] = T(0); (*this)[2][2] = T(-2)/depth; (*this)[3][2] = -(far + near)/depth; //and here
(*this)[0][3] = T(0); (*this)[1][3] = T(0); (*this)[2][3] = T(0); (*this)[3][3] = T(1);
return *this;
}
Base Matrix类:
template<typename T, unsigned int D>
class Matrix
{
public:
Matrix() { }
virtual ~Matrix() { }
Matrix(const Matrix& other) { *this = other; }
inline Matrix InitIdentity(); //defined in the same header, but left out here to save space
inline Matrix InitTranslation(const Vector<T, D-1>& r);
inline Matrix& operator=(const Matrix& rhs);
inline Matrix operator*(const Matrix<T,D>& r) const;
inline const T* operator[](int index) const { return m[index]; }
inline T* operator[](int index) { return m[index]; }
private:
T m[D][D];
};
在基类"Matrix"中没有错误,只有在派生类"Matrix4"中有错误。
如果代码看起来很熟悉,那是因为我正在学习Youtube教程
找到解决方案了!谢谢你的帮助!
我把我的参数变量名从"far" &"near"到"m_near" &"m_far",现在它工作了。我认为这可能是与代码中某个类中的#define或其他方法冲突。
编译和运行没有错误。我找不到导致冲突/错误的原始问题的原因。改变似乎已经解决了这个问题,所以我不认为需要太长时间或努力。
const,在方法内的变量声明之前,似乎还没有产生任何不希望的效果,所以我将保留它。修复了Matrix4类的代码:
inline Matrix4<T> InitOrthographic(T left, T right, T m_near, T m_far, T bottom, T top)
{
const T width = right - left;
const T height = top - bottom;
const T depth = m_far - m_near;
(*this)[0][0] = T(2)/width; (*this)[1][0] = T(0); (*this)[2][0] = T(0); (*this)[3][0] = -(right + left)/width;
(*this)[0][1] = T(0); (*this)[1][1] = T(2)/height; (*this)[2][1] = T(0); (*this)[3][1] = -(top + bottom)/height;
(*this)[0][2] = T(0); (*this)[1][2] = T(0); (*this)[2][2] = T(-2)/depth; (*this)[3][2] = -(m_far + m_near)/depth;
(*this)[0][3] = T(0); (*this)[1][3] = T(0); (*this)[2][3] = T(0); (*this)[3][3] = T(1);
return *this;
}
试一下,它在MS Visual c++下编译成功
template<typename T, unsigned int D>
class Matrix
{
public:
Matrix() { }
virtual ~Matrix() { }
Matrix(const Matrix<T, D>& other) { *this = other; }
inline Matrix<T, D> InitIdentity(); //defined in the same header, but left out here to save space
inline Matrix<T, D> InitTranslation(const Vector<T, D-1>& r);
inline Matrix<T, D>& operator=(const Matrix<T, D>& rhs);
inline Matrix<T, D> operator*(const Matrix<T,D>& r) const;
inline const T* operator[](int index) const { return m[index]; }
inline T* operator[](int index) { return m[index]; }
private:
T m[D][D];
};
template<typename T>
class Matrix4 : public Matrix<T, 4>
{
public:
Matrix4() { }
inline Matrix4<T> InitOrthographic(T left, T right, T bottom, T top, T near, T far)
{
const T width = (right - left);
const T height = (top - bottom);
const T depth = (far - near); //error occurs here
(*this)[0][0] = T(2)/width; (*this)[1][0] = T(0); (*this)[2][0] = T(0); (*this)[3][0] = -(right + left)/width;
(*this)[0][1] = T(0); (*this)[1][1] = T(2)/height; (*this)[2][1] = T(0); (*this)[3][1] = -(top + bottom)/height;
(*this)[0][2] = T(0); (*this)[1][2] = T(0); (*this)[2][2] = T(-2)/depth; (*this)[3][2] = -(far + near)/depth;
(*this)[0][3] = T(0); (*this)[1][3] = T(0); (*this)[2][3] = T(0); (*this)[3][3] = T(1); //and here
return *this;
}
};
我不知道Vector
类发生了什么,因为我用int
相关文章:
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- C++ 标记之前预期主表达式'('错误
- 正则表达式中带有卷轴大括号的正则表达式错误
- C++初学者错误:主要表达式错误
- 代数树表达式C++错误:未解析的外部符号"Public:_thiscall AlgebraicTreeExpression::~AlgebraicTreeExpression(void) ..."
- 使用多线程处理对象数组 - 无效使用 void 表达式错误
- 为什么 if constexpr 不会使这个核心常量表达式错误消失?
- 提升正则表达式错误:未初始化的提升::match_results
- C++比较表达式错误
- C++ "主表达式"错误
- C++ 正则表达式错误(未处理的异常)
- 'this'不能用于常量表达式错误 (C++)
- 代码块引发有关for_each的预期主表达式错误
- Boost Regex中的命名子表达式错误
- C++之前的主要表达式错误
- Ubuntu 上的正则表达式错误
- C++正则表达式错误
- 理解常量表达式错误
- 成员函数和预期的主表达式错误
- Xcode中应有表达式错误