调用类的函数时出错

时间:2014-07-17 17:13:11

标签: c++ class methods member

我刚开始为一个类编写一些代码,这将是一个分析引擎,现在很简单,因为我已经掌握了我可以用我导入的库(bpp代码)做的事情:< / p>

#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

也许我很厚,但我不明白为什么当我将它定义为myEngine的公共成员时,它不会让我执行LoadSeq,为什么它从myEngine的hi实例请求它时会出错?

谢谢, 本W。

3 个答案:

答案 0 :(得分:9)

此:

myEngine hi();

声明一个函数,而不是一个对象。要声明和默认构造对象,请删除括号:

myEngine hi;

答案 1 :(得分:0)

本声明

myEngine hi();

是一个名为hi的函数声明,其返回类型为myEngine且没有参数。

改为写

myEngine hi;

myEngine hi {};

考虑到你没有定义默认构造函数和析构函数(至少你没有显示它们的定义)。代码将编译你的couls定义它如下

class myEngine
{
public:
    myEngine() = default;
    ~myEngine() = default;
    //...

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

答案 2 :(得分:0)

在这一行:

myEngine hi();

声明一个函数hi,它返回myEngine类型的实例。

将其更改为:

myEngine hi()

myEngine hi = myEngine();