- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试用 C++ 实现一个 Vector4 类和一个 Matrix4x4 类,以便更好地掌握这门语言。我环顾四周,似乎没有什么能真正解决我遇到的问题,如果我遗漏了什么,我深表歉意。
编辑:原来的错误似乎不再发生(这是由循环包含引起的)。但是,现在我收到以下错误:
1>main.cpp(35): error C2064: term does not evaluate to a function taking 2 arguments
我只能想象这是因为我在 CMatrix4x4 中重载了 () 运算符,但是在我以前的代码中从 main() 调用时并没有发生这种情况。
请求的 SSCCE 案例:
#include <assert.h>
#include <cmath>
#include <iostream>
class CMatrix4x4;
class CVector4
{
public:
float x, y, z, w;
CVector4();
CVector4(float, float, float, float);
~CVector4();
CVector4 operator*(CMatrix4x4&);
};
CVector4::CVector4()
{
x, y, z, w = 0;
}
CVector4::CVector4(float cx, float cy, float cz, float cw)
{
x = cx, y = cy, z = cz, w = cw;
}
//No instance of overloaded function "CVector4::operator" matches the specified type
//<error-type> m
//DOES NOT occur with forward declaration of class, only when including matrix.h
//from a separate file.
//Now causes "term does not evaluate to a function taking 2 arguments" at lines: 35-38
//Whenever I call the overloaded operator ()
CVector4 CVector4::operator*(CMatrix4x4& m)
{
CVector4 v;
v.x = x*m(0, 0) + y*m(1, 0) + z*m(2, 0) + w*m(3, 0);
v.y = x*m(0, 1) + y*m(1, 1) + z*m(2, 1) + w*m(3, 1);
v.z = x*m(0, 2) + y*m(1, 2) + z*m(2, 2) + w*m(3, 2);
v.w = x*m(0, 3) + y*m(1, 3) + z*m(2, 3) + w*m(3, 3);
return v;
}
class CMatrix4x4
{
public:
CMatrix4x4();
~CMatrix4x4();
void SetRow(int r, CVector4);
float operator()(int r, int c);
private:
float matrix4x4[4][4];
};
CMatrix4x4::CMatrix4x4()
{
for(int r = 0; r < 4; r++)
{
for(int c = 0; c < 4; c++)
{
matrix4x4[r][c] = 0;
}
}
}
CMatrix4x4::~CMatrix4x4()
{
}
float CMatrix4x4::operator()(int r, int c)
{
assert(r >= 0 && r < 4);
assert(c >= 0 && c < 4);
return matrix4x4[r][c];
}
void CMatrix4x4::SetRow(int r, CVector4 v)
{
assert(r >= 0 && r < 4);
matrix4x4[r][0] = v.x;
matrix4x4[r][1] = v.y;
matrix4x4[r][2] = v.z;
matrix4x4[r][3] = v.w;
}
int main()
{
CMatrix4x4 m;
CVector4 vec1(1, 2, 3, 4);
CVector4 vec2;
m.SetRow(0, CVector4(1, 0, 0, 0));
m.SetRow(1, CVector4(0, 1, 0, 0));
m.SetRow(2, CVector4(0, 0, 1, 0));
m.SetRow(3, CVector4(0, 0, 0, 1));
vec2 = vec1 * m;
std::cout << vec2.x;
std::cin.ignore();
return 0;
}
编辑:感谢所有提供帮助的人。我设法通过将函数实现移动到单独的 .cpp 文件来解决这个问题(我应该首先这样做。我不知道为什么我不这样做),并在其中包含所需的 header ,并在 header 文件中使用前向声明.
我不确定这是否是正确的解决方案,但它看起来确实有效。
最佳答案
与之前提出的许多问题相同的问题:代码的原始版本显然有两个头文件 - vector.h
和 matrix.h
- 它们相互包含.这是循环包含,没有任何意义。
您的头文件中可能包含的 include guards 确保包含不会变成无限的。但是它们对解决数据类型之间的循环依赖没有任何作用。 CMatrix4x4
在您的 vector.h
中是完全未知的,这会导致错误。
vector.h
中CMatrix4x4
的前向声明是朝着正确方向迈出的一步。但是,您无论如何都必须摆脱那个无用的循环包含。你必须记住 CMatrix4x4
在 vector.h
中是一个不完整的类型,这意味着你将无法在 vector 中访问它的内部结构。 h
.
后者意味着您的 CVector4 CVector4::operator*(CMatrix4x4& m)
必须在CMatrix4x4
的定义之后定义,而不是前。在您的代码中,它在 CMatrix4x4
之前定义。此时 CMatrix4x4
类型仍然不完整,这意味着您不能使用它的 ()
运算符。像 m(0, 0)
这样的表达式不会因为这个原因而被编译。这就是您收到错误的原因。
附言此外,
x, y, z, w = 0;
并不像您认为的那样。它会将 0
分配给 w
,但保持其他数据成员不变(阅读 C++ 中的逗号运算符)。
关于c++ - 没有重载函数的实例 "CVector4::operator"匹配指定的类型/术语不计算为采用 2 个参数的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14166169/
有没有一种方法可以使用标准类型构造函数(例如 int、set、dict、list、tuple 等)以用户定义的方式将用户定义类的实例强制转换为其中一种类型?例如 class Example:
我知道这个问题在Stackoverflow中有很多问题,但是即使有很多答案,这些答案也帮不了我什么,也没有找到答案。 在我的WebAPP中,它可以正常工作,但是当我将其转换为API时,它失败了(主题标
这个问题已经有答案了: Why does the ternary operator unexpectedly cast integers? (3 个回答) 已关闭 9 年前。 最近遇到一个Java的陷
我尝试使用 FirebaseApp.configure() 配置 Firebase,但遇到以下崩溃: *** Terminating app due to uncaught exception 'c
我有一个自连接员工实体类,其中包含与其自身相关的 id、name 和 ref 列。我想创建它的新实例并将其保存到数据库。 首先我创建了一个 Employee 类的实例并将其命名为 manager。然后
我有一个用于添加新公寓的表单,在该表单中我有一个下拉列表,用户可以在其中选择负责的人员。 显然,当您从下拉列表中选择并尝试保存公寓时,我的应用程序认为该人已被修改。它给了我下面的错误,指示我应该首先保
从 Visualforce 页面,我需要检索我们组织的 salesforce 实例的 URL,而不是 Visual Force URL。 例如我需要https://cs1.salesforce.com
我遇到了一些可能的问题答案,但这是关于从 Hibernate 3.4.0GA 升级到 Hibernate 4.1.8 的问题。所以这曾经在以前的版本下工作,我已经四处搜索了为什么它在这个新版本中出现了
似乎一遍又一遍地问这个问题,我仍然找不到解决我问题的答案。我在下面有一个域模型。每个新创建或更新的“安全用户”都需要我确保其具有配置文件,如果没有,则创建一个新的配置文件并分配给它。 配置文件的要求相
我很难调试为什么 JPA 不级联我的 @ManyToMany 关系。我发现的所有答案都与缺少级联语句有关。但我确实拥有它们并且仍然得到: Caused by: org.hibernate.Transi
Play 服务 API 表明有一个叫做 Instance ID 的东西 但是,在 Android Studio 中包含以下内容后,我无法导入 InstanceID 类 compile "com.goo
我正在使用 Seam 框架。我有 2 个实体: 请求.java @Entity @Table(name = "SRV_REQUEST") public class Request { private
This question处理构建一个适当的Monad来自单子(monad)的实例,但仅在某些约束下 - 例如Set .诀窍是将其包装成 ContT ,它将约束推迟到包装/展开其值。 现在我想对 Ap
我正在尝试执行此查询: StringBuffer sb = new StringBuffer(); sb.append("select p from PointsEntity p " + "where
我试图了解是否可以更改我的 hibernate 配置并使用单个 MySQL 实例(而不是我当前拥有的多个 MySQL 实例): 我有一个使用 hibernate 的 Java 应用程序,与 2 个模式
我有一个选项卡滑动布局,其中包括四个选项卡,每个选项卡都有自己的布局和 fragment ,在我的主要 Activity 布局中,viewpager 参与更改选项卡。特定 View (选项卡)在应用程
我看到很多帖子声称他们正在运行 MySql 的 RDS 实例,但无法连接到该实例,但我没有运行 RDS。 我使用 EC2 实例来托管我的 WordPress 博客,该博客是使用 Web 平台安装程序安
因为我在我的 ec-2 实例上的 python 虚拟环境中运行应用程序( Airflow ),并且我想在同一个 ec2 实例上的默认 python 环境中运行命令,所以我认为 ssh 到我自己的实例更
这个问题已经有答案了: How to fix the Hibernate "object references an unsaved transient instance - save the tra
例子: run APP1 .. ... run APP1 ... run APP2 如何在 APP2 中对 Vue 说我需要调用 APP1?
我是一名优秀的程序员,十分优秀!