调用类的函数出错

Error calling function of a class

本文关键字:出错 函数 调用      更新时间:2023-10-16

我刚刚开始为一个类编写一些代码,这将是一个分析引擎,现在很简单,因为我已经掌握了我可以用我导入的库(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;
    //...

或者您可以删除这些声明(和定义),并使用编译器隐式定义的构造函数和析构函数