Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Error: expected a type' creating a new instance

Tags:

c++

linux

I have a problem and I hope you can help me. I'm creating a C++ program which is running on linux. I have defined two classes, where the main one is called Downloader and looks like this:

 #ifndef __DOWNLOADER_H__
 #define __DOWNLOADER_H__
 #include "configDownloader.h"
 #include "mailServer.h"
 #include <logger.h>

 using namespace ObjectModel;

 namespace Downloader
 {
     class Downloader
     {
        private:
                ...
                MailServer *m_mailServer;

        public:
                Downloader(char* configFileName);
                ...
     }; 
 }
 #endif

In the constructor of this class, I have tried to create a new instance of the class MailServer, which I defined into the same Namespace. The code of MailServer looks this way:

#ifndef __MAILSERVER_H__
#define __MAILSERVER_H__

#include <stdio.h>
#include <list>
#include <mail.h>

using namespace std;
using namespace ObjectModel;

namespace Downloader
{
    class MailServer
    {
        private:        
            list<Mail> m_mails;
            char *m_username;
            char *m_password;

        public:
            MailServer();
            ~MailServer();

            void Open(char *username,char *password);
            bool SaveEmails(char *pathFiles);
            void Close();
    }; 
}
#endif

The constructor of this class is defined correctly into the .cpp file and everything seems correct. The problem is that when I try to create a new instance of MailServer inside the constructor of Downloader, the compiler says "error: expected a type"

#include <stdio.h>
#include "downloader.h"
#include <unistd.h>

namespace Downloader
{
    Downloader::Downloader(char* fileName)
    {
        this->m_config = new ConfigDownloader(fileName);
        this->m_log = new Logger("Log",LOG_LEVEL_INFO,0);
        this->m_mailServer = new MailServer();//the compiler shows the error right here
    }
 ...

any ideas? I read somewhere that it could be that the compiler is too old, but I don't feel really comfortably coding inside the makefile.

like image 473
Mauro Bilotti Avatar asked Mar 22 '26 01:03

Mauro Bilotti


1 Answers

I found the solution! I'm sorry because i didn't see it. The problem was that i defined a public getter to return the private field like this:

class Downloader
{
    private:
        ConfigDownloader* m_config;
        Logger* m_log;
        MailServer *m_mailServer;

    public:
        MailServer *MailServer();

being that both was define into the same scope, the compiler could be confused about the constructor of the class and the method, because both were called with the same name. The problem was mine! But everyone should take care about this, because intellisense doesn't tells you anything about it

like image 119
Mauro Bilotti Avatar answered Mar 23 '26 17:03

Mauro Bilotti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!