stefv
stefv

Reputation: 439

Use of a function name in a namespace in C++?

iI would like to create a class Console in a namespace system. I defined the class like that:

Declaration

#pragma once
namespace system
{
    class Console
    {
       public:
          ~Console();
          void static writeLine();
       private:
          Console();
    };
}

Definition

#include "Console.h"
#include <iostream>

system::Console::Console() {}
system::Console::~Console() {}
void system::Console::writeLine() {
    std::cout << "test" << std::endl;
}

But the compiler is refusing to define this class because system is already defined in stdlib.h (included with iostream).

Do I have a solution to force the compiler to "ignore" the first definition of system ?

Thank you.

Stef

Upvotes: 0

Views: 67

Answers (1)

user7860670
user7860670

Reputation: 37597

upper level namespace names should be reasonably unique (instead of being generic) and not clash not only with standard library but also with the rest of libraries out there. So just put namespace system into another namespace or rename it to something like

namespace stefv::system
{

Additionally you may want to use some sort of prefix specific for namespace names. For example ns_stefv::ns_system.

Upvotes: 5

Related Questions