【问题标题】:Declaration of an object in another header file在另一个头文件中声明一个对象
【发布时间】:2016-10-20 09:20:58
【问题描述】:

) 我有一个很容易解决的问题,但我在 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_nonlstiffness_nonl的声明; ??? stiff_nonl 类继承自另一个类有什么关系????我必须在声明中考虑这一点吗?

希望其中一位 c++ 检查器可以帮助我吗?

非常感谢!

弗兰茨干杯

【问题讨论】:

  • stiff_nonl 一个默认构造函数(没有参数)或正确的参数可能会解决你的问题。注意:通常的做法是在命名类型时将每个单词的第一个字符大写,stiff_nonl 可能应该是 Stiff_Nonl
  • 可能将stiffness_nonl添加到new_raphs的构造函数的初始化列表中会有所帮助(George所示的问题解决方案略有不同)

标签: c++ object declaration


【解决方案1】:

stiff_nonl 没有默认构造函数。您可以在 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

【讨论】:

    【解决方案2】:

    哇,谢谢!现在可以了:)

    正确的构造函数是:

    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)???这对我来说有点奇怪......

    弗兰茨干杯

    【讨论】:

    • 你需要阅读c++/面向对象的编程书籍。在第一种情况下,您将参数传递给基类(set_force),第二种情况是类字段(stiffness_nonl)的初始化。阅读类和对象(类的实例)之间的区别,还可以了解有关继承的更多信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 2021-11-09
    相关资源
    最近更新 更多