另一个头文件中对象的声明
Declaration of an object in another header file
(我有一个问题可以很简单地解决,但我在c++中找不到正确的形式主义。我想将另一个类的对象声明为给定的头文件。不幸的是,我得到了错误:
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
calc/solver_nonl/new_raphs.cpp: In constructor
‘new_raphs::new_raphs(Eigen::MatrixXd, Eigen::MatrixXd, Eigen::MatrixXd, Eigen::MatrixXd, Eigen::MatrixXd)’:
calc/solver_nonl/new_raphs.cpp:7:44: error: no matching function for call to ‘stiff_nonl::stiff_nonl()’
:set_force(node_matrix,dof_matrix_input)
^
calc/solver_nonl/new_raphs.cpp:7:44: note: candidates are:
In file included from calc/solver_nonl/new_raphs.h:8:0,
from calc/solver_nonl/new_raphs.cpp:1:
calc/solver_nonl/../fem_nonl/stiff_nonl.h:20:5: note: stiff_nonl::stiff_nonl(Eigen::MatrixXd, Eigen::MatrixXd, Eigen::MatrixXd, Eigen::MatrixXd)
stiff_nonl(MatrixXd node_matrix, MatrixXd dof_matrix_input, MatrixXd element_matrix, MatrixXd element_info_matrix);
^
calc/solver_nonl/../fem_nonl/stiff_nonl.h:20:5: note: candidate expects 4 arguments, 0 provided
calc/solver_nonl/../fem_nonl/stiff_nonl.h:17:7: note:
stiff_nonl::stiff_nonl(const stiff_nonl&)
class stiff_nonl : public set_force
^
calc/solver_nonl/../fem_nonl/stiff_nonl.h:17:7: note: candidate expects 1 argument, 0 provided
calc/solver_nonl/new_raphs.cpp: In member function ‘Eigen::MatrixXd& new_raphs::get_epsilon_results(int, int)’:
calc/solver_nonl/new_raphs.cpp:92:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
make: *** [all] Fehler 1
我以前经常声明不同类的对象,但我在这里做错了什么?
头文件如下所示:
#ifndef NEW_RAPHS_H
#define NEW_RAPHS_H
# include "../../pre/boundary_force/set_force.h"
# include <iostream>
#include "../fem_lin/stiff.h"
#include "../fem_nonl/stiff_nonl.h"
using namespace std;
class new_raphs : set_force
{
public:
new_raphs(MatrixXd node_matrix, MatrixXd dof_matrix_input, MatrixXd element_matrix, MatrixXd element_info_matrix, MatrixXd force_matrix_input);
MatrixXd elements;
MatrixXd elements_info;
MatrixXd force_matrix;
void calc();
VectorXd U;
MatrixXd res;
MatrixXd result_matrix;
MatrixXd &get_result();
stiff_nonl stiffness_nonl; // here is the bug!
MatrixXd &get_epsilon_results(int noe, int num_gauss_point);
};
#endif // NEW_RAPHS_H
他显然不喜欢这个宣言???stiff_noll类从另一个类继承是否与此有关????我必须在声明中考虑这一点吗?
希望有一个c++检查器能在这里帮我?
非常感谢!
Franz 干杯
stiff_noll没有默认构造函数。您可以在new_raphs构造函数初始化列表中传递正确的参数,例如:
new_raphs::new_raphs(MatrixXd node_matrix, MatrixXd dof_matrix_input, MatrixXd element_matrix, MatrixXd element_info_matrix, MatrixXd force_matrix_input)
: stiffness_nonl() //pass arguments to stiff_nonl constr. inside brackets
{
//...
}
此外,我相信你想要这里的公共继承:
class new_raphs : public set_force
哇,谢谢!现在它工作了:(
正确的构造函数是:
new_raphs::new_raphs(MatrixXd node_matrix, MatrixXd dof_matrix_input, MatrixXd element_matrix, MatrixXd element_info_matrix, MatrixXd force_matrix_input)
:set_force(node_matrix,dof_matrix_input), stiffness_nonl(node_matrix, dof_matrix_input, element_matrix, element_info_matrix)
我仍然有点困惑,为什么对象名称(stiffness_nonl(而不是类或构造函数名称(stif_noll(???这对我来说有点奇怪…
Franz 干杯
相关文章:
- Arduino C++在构造函数中用参数声明对象数组
- 为什么我在声明对象数组时不能使用 -> 运算符?
- 如何在 if 语句中声明对象并在任何我想的地方使用它?
- 如何使用unique_ptr和make_unique正确声明对象数组
- 声明对象时,如何在C 中修复C2065错误
- 声明对象而不调用默认构造函数
- 一旦对象设置为 null,用于声明对象的堆大小的变量就设置为 null?
- 我可以使用名称而不是类型来声明对象文字吗?
- 在制作(Qt等)GUI时声明对象
- 在基本的GUI编程中,在哪里声明对象和方法
- 同时使用模板和参数声明对象
- "Undefined reference to"使用双模板类声明对象时出错
- 在头文件 C++ 中声明对象
- 在其类中声明对象
- C 可以通过使用要求其为某个类别的对象来声明对象
- 声明对象
- (C++)如何基于条件声明对象类成员
- 使用变量声明对象的名称
- C++ OOP 体系结构:决定是从基类声明对象还是继承基类
- 在声明对象之前在全局函数中使用类的成员函数