首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在另一个头文件中声明对象

在另一个头文件中声明对象
EN

Stack Overflow用户
提问于 2016-10-20 17:20:58
回答 2查看 214关注 0票数 0

)我有一个问题很容易解决,但我在c++中找不到正确的形式主义。我想在给定头文件中声明另一个类的对象。不幸的是,我得到了这个错误:

代码语言:javascript
运行
复制
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

我以前经常声明不同类的对象,但我在这里做错了什么?

头文件如下所示:

代码语言:javascript
运行
复制
#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_nonl stiffness_nonl;?它是否与stiff_nonl类从其他类继承的操作有关?我必须在声明中考虑这一点吗?

希望某个c++跳棋能帮到我?

首先要感谢大家!

干杯Franz

EN

回答 2

Stack Overflow用户

发布于 2016-10-20 17:42:23

stiff_nonl没有默认构造函数。您可以在new_raphs构造函数初始化列表中传递正确的参数,例如:

代码语言:javascript
运行
复制
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
{
    //...
}

另外,我相信你想在这里公开继承:

代码语言:javascript
运行
复制
class new_raphs : public set_force
票数 0
EN

Stack Overflow用户

发布于 2016-10-20 20:30:51

哇,谢谢!现在它可以工作了:)

正确的构造函数是:

代码语言:javascript
运行
复制
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)而不是类或构造函数名(Stiff_nonl)?这对我来说有点奇怪。

干杯Franz

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40150347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档