1

I have my header file which look like this

#ifndef NORMALCLIENT_H
#define NORMALCLIENT_H

#include <ClientType.h>

class NormalClient : public ClientType {
public:
    NormalClient();
    NormalClient(const NormalClient& orig);
    double getDiscount();
    int getMaxRents();
    virtual ~NormalClient();
private:

};

#endif /* NORMALCLIENT_H */

And source file:

#include "NormalClient.h"

NormalClient::NormalClient() {
}

NormalClient::NormalClient(const NormalClient& orig) {
}

NormalClient::~NormalClient() {
}

Is there a way of not repeating NormalClient:: in source files? I tried:

#include "NormalClient.h"

using namespace NormalClient

NormalClient() {
}

NormalClient(const NormalClient& orig) {
}

~NormalClient() {
}

But it gives me errors during compilation:

/home/grayrattus/projekt/trunk/trunk/biblioteka/src/model/client_types/NormalClient.cpp:3:17: error: ‘NormalClient’ is not a namespace-name
 using namespace NormalClient
                 ^
/home/grayrattus/projekt/trunk/trunk/biblioteka/src/model/client_types/NormalClient.cpp:5:1: error: expected namespace-name before ‘NormalClient’
 NormalClient() {
 ^

I don't understand why compiler gives errors of namespace when I define it before.

Question: Is there a way of not repeating namespaces in source files?

A. Dziedziczak
  • 153
  • 3
  • 10
  • 4
    NormalClient is not a namespace, with that word you are indicating that those functions are methods of the class. – eyllanesc Dec 03 '17 at 03:58
  • 1
    What you're repeating is a class name, not a namespace name. To reduce the verbosity you can use an alias for the type, except for constructor and destructor definitions. In modern C++ you define a type alias via a `using` directive, while in C++03 and C you use a `typedef`. – Cheers and hth. - Alf Dec 03 '17 at 03:59

2 Answers2

3

Compiler need to know where's definition corresponding the class declaration.

You just declared the class at the file "ClientType.h". On the other hand, you need to define the class and it would be located at cpp file normally.

(about declaration and definition, see https://stackoverflow.com/a/1410632/9043064)

If you want reduce to repeating source code, then you can just define class at your header file

class NormalClient : public ClientType {
public:
    NormalClient() { // implementation }
    NormalClient(const NormalClient& orig) { // implementation }
    double getDiscount() { // implementation }
    int getMaxRents() { // implementation }
    virtual ~NormalClient() { // implementation }
private:

};

But it is not recommended in that header file's getting bigger. it could increase building time. And also it's not a good practice. (https://stackoverflow.com/a/333964/9043064)

About using namespace... I think it'll be helpful for you to read this (even though it's not possible to use actually for implementing class definition) Why is "using namespace std" considered bad practice?

kayjay
  • 44
  • 3
1

using namespace NormalClient; is the cause of the error. NormalClient is a class, not a namespace. You don't have to use the using namespace to use the NormalClient header, you have to create an instance of the class and access stuff inside using the dot operator, eg: NormalClient.something.

  • So I need to repeat `NormalClient::` all the time in source code and there is no way of make it cleaner? That's because `NormalClient` as @eyllanesc wrote _with that word you are indicating that those functions are methods of the class_ ? – A. Dziedziczak Dec 03 '17 at 04:06