compile error

G

Gary Wessle

Hi

I am getting many similar compile errors which look like this
User.h:9: error: ISO C++ forbids declaration of ‘Account’ with no type

I cannot find out why. can you please help?

thanks

// User.h
#ifndef USER_H
#define USER_H
#include "Account.h"

class User{
public:
User();
// ~User();
Account* getAccountWithId(const int accId); //line 9
};
#endif // USER_H

//Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H

class Account {
public:
Account();
// ~Account();
};

#endif // ACCOUNT_H
 
D

Daniel T.

Gary Wessle said:
Hi

I am getting many similar compile errors which look like this
User.h:9: error: ISO C++ forbids declaration of ‘Account’ with no type

I cannot find out why. can you please help?

I don't see anything especially wrong with what you posted. Could it be
that your "Account.h" has a line like: #include "User.h" ?
 
S

Salt_Peter

Gary said:
Hi

I am getting many similar compile errors which look like this
User.h:9: error: ISO C++ forbids declaration of 'Account' with no type

I cannot find out why. can you please help?

thanks

// User.h
#ifndef USER_H
#define USER_H
#include "Account.h"

class User{
public:
User();
// ~User();
Account* getAccountWithId(const int accId); //line 9
};
#endif // USER_H

//Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H

class Account {
public:
Account();
// ~Account();
};

#endif // ACCOUNT_H

In order for the compiler to successfully generate any of the above
class types, you need to implement those 2 def ctors since you've
overriden the compiler's ability to generate and implement them for
you. Hence the error.

So:

// User.cpp
#include "User.h"

User::User()
{
}

Account* User::getAccountWithId(const int accId)
{
return 0; // for now
}

// Account.cpp
#include "Account.h"

Account::Account()
{
}

And in the case you need User::getAccountWithId(...) to respect
constant-correctness, and depending on the container employed, use a
const reference instead:

const Account& User::getAccount(const int accId) const
{
return vacc.at(accId); // vector of accounts or whatever
}

If you prefer for some unknown reason to do it with dumb pointers, at
least protect the returned pointer:

Account* const User::getAccount(const int accId) const
{
return 0; // for now
}
 
J

Jim Langston

Gary Wessle said:
Hi

I am getting many similar compile errors which look like this
User.h:9: error: ISO C++ forbids declaration of 'Account' with no type

I cannot find out why. can you please help?

thanks

// User.h
#ifndef USER_H
#define USER_H
#include "Account.h"

class User{
public:
User();
// ~User();
Account* getAccountWithId(const int accId); //line 9
};
#endif // USER_H

//Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H

class Account {
public:
Account();

Where's the body? Either you need a
Account::Account() { /* ... */ }
somewhere, or change this to
Account() {};
 
P

Pete Becker

Gary said:
Hi

I am getting many similar compile errors which look like this
User.h:9: error: ISO C++ forbids declaration of ‘Account’ with no type

I cannot find out why. can you please help?

thanks

// User.h
#ifndef USER_H
#define USER_H
#include "Account.h"

class User{
public:
User();
// ~User();
Account* getAccountWithId(const int accId); //line 9
};
#endif // USER_H

//Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H

class Account {
public:
Account();
// ~Account();
};

#endif // ACCOUNT_H

Both headers compile fine for me, as they ought to. The problem must be
in the code that you didn't show. (Despite two responses saying it, you
don't need to have definitions for any of these members in order to
compile these headers)

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
J

JE

Gary said:
Hi

I am getting many similar compile errors which look like this
User.h:9: error: ISO C++ forbids declaration of 'Account' with no type

I cannot find out why. can you please help?

thanks

// User.h
#ifndef USER_H
#define USER_H
#include "Account.h"

class User{
public:
User();
// ~User();
Account* getAccountWithId(const int accId); //line 9
};
#endif // USER_H

//Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H

class Account {
public:
Account();
// ~Account();
};

#endif // ACCOUNT_H

You may have a typo (e.g. #ifdef ACCOUNT_H).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,244
Members
46,838
Latest member
KandiceChi

Latest Threads

Top