调用类的函数出错
Error calling function of a class
我刚刚开始为一个类编写一些代码,这将是一个分析引擎,现在很简单,因为我已经掌握了我可以用我导入的库(bpp)做的事情:
#include <string>
#include <iostream> //to be able to output stuff in the terminal.
#include <Bpp/Seq/Alphabet.all> /* this includes all alphabets in one shot */
#include <Bpp/Seq/Container.all> /* this includes all containers */
#include <Bpp/Seq/Io.all> /* this includes all sequence readers and writers */
class myEngine
{
public:
myEngine();
~myEngine();
void LoadSeq();
};
void myEngine::LoadSeq()
{
bpp::Fasta fasReader;
bpp::AlignedSequenceContainer *sequences = fasReader.readAlignment("tester.fasta", &bpp::AlphabetTools::DNA_ALPHABET);
std::cout << "This container has " << sequences->getNumberOfSequences() << " sequences." << std::endl;
std::cout << "Is that an alignment? " << (bpp::SequenceContainerTools::sequencesHaveTheSameLength(*sequences) ? "yes" : "no") << std::endl;
}
int main()
{
myEngine hi();
hi.LoadSeq();
return 0;
}
我没有定义构造函数或析构函数,因为它们现在没有参数,也没有任何成员变量,除了一个成员函数,它不返回任何东西,只是加载一个文件并输出到cout。
但是尝试编译不工作:
rq12edu@env-12bw:~/Desktop/TestingBio++$ make
g++ main.cpp -o mainexec --static -I/local/yrq12edu/local/bpp/dev/include -L/local/yrq12edu/local/bpp/dev/lib -lbpp-seq -lbpp-core
main.cpp: In function 'int main()':
main.cpp:26:5: error: request for member 'LoadSeq' in 'hi', which is of non-class type 'myEngine()'
make: *** [all] Error 1
也许我是厚,但我不明白为什么它不让我执行LoadSeq当我把它定义为myEngine的公共成员,为什么它是错误的,当它从myEngine的hi实例请求它?
谢谢,本·w .
This:
myEngine hi();
声明一个函数,而不是一个对象。要声明和默认构造一个对象,请删除括号:
myEngine hi;
这一行:
myEngine hi();
你声明一个函数hi
返回myEngine类型的实例。
修改为:
myEngine hi()
或
myEngine hi = myEngine();
语句
myEngine hi();
是一个函数声明,名称为hi
,返回类型为myEngine
,没有参数。
写而不是
myEngine hi;
或
myEngine hi {};
请考虑您没有定义默认构造函数和析构函数(至少您没有显示它们的定义)。你可以用下面的方式定义代码
class myEngine
{
public:
myEngine() = default;
~myEngine() = default;
//...
或者您可以删除这些声明(和定义),并使用编译器隐式定义的构造函数和析构函数
相关文章:
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 从R调用C++函数并对其进行集成时出错
- 这个函数哪里出错了
- PrintWindow函数在notepad.exe中出错
- 使用运算符调用 void 函数时出错<set>
- 为 NewObjectA() 函数创建 jvalues 的参数数组时出错 - JNI Invocation API
- 在定义的函数中使用常量时出错
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 使用根/C++时出错:没有匹配的构造函数来初始化'TTree'
- Arduino IDE 中使用 strncmp 函数时出错
- 从DLL导出函数,LoadLibrary()需要用TEXT转换的字符串才能编译而不会出错
- 编译包含指向模板函数的指针的初始值设定项列表时,gcc 出错,但 clang 不出错
- 使用一个参数的模板函数时出错(适用于 2)
- 为什么 stl 字符串C++函数有时出错有时会出错?
- 使用类的构造函数时出错
- 在 c++ 的构造函数中分配对象向量时出错
- 为什么在函数参数前面添加 const 会出错?
- 调用析构函数时出错