SJWard
SJWard

Reputation: 3739

Error calling function of a class

I just started writing some code for a class what will be an engine for an analysis, it's simple right now since I'm getting to grips with the stuff I can do with the libraries I import (the bpp ones):

#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;
}

I've not defined a constructor or destructor since they take no arguments right now and there aren't any member variable except a member function which returns nothing, just loads a file and prints to cout.

However trying to compile does not work:

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

Maybe I'm being thick but I don't see why it's not letting me execute LoadSeq when I've defined it as a public member of myEngine, why is it erroring when it requests it from the hi instance of myEngine?

Thanks, Ben W.

Upvotes: 0

Views: 315

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 310910

This statement

myEngine hi();

is a function declaration with name hi having return type myEngine and no parameters.

Write instead

myEngine hi;

Or

myEngine hi {};

Take into account that you did not define the default constructor and the destructor (at least you did not show their definitions). That the code would ve compiled you couls define it the following way

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

Or you could remove these declarations (and definitions) and use implicitly defined by the compiler constructor and destructor.

Upvotes: 0

Slava
Slava

Reputation: 44238

In this line:

myEngine hi();

You declare a function hi that returns instance of type myEngine.

Change it either to:

myEngine hi()

or

myEngine hi = myEngine();

Upvotes: 0

Mike Seymour
Mike Seymour

Reputation: 254431

This:

myEngine hi();

declares a function, not an object. To declare and default-construct an object, remove the parentheses:

myEngine hi;

Upvotes: 9

Related Questions