1

I'm a begginer with C++ and I'm trying to practice with headers and Namespaces. Specifically I wanted to create different modules with their own namespaces so I can use one program to go through others without mixing the names. I used the advice from this other question and it works fine with functions but when I get to a constant I get an "redefining error"

The 2 relevant files:


//mainHeader.h
#pragma once
#include <iostream>
#include <string>
#include <sstream>
namespace primes {
    int isPrime(int a);
}
namespace CONST {
    typedef unsigned long long int LONGINT;
    double pi;
}
namespace E1 {
    int main();
}
namespace E2 {
    int main();
}

//CONSTANTS.cpp
#include "mainHeader.h"
namespace CONST{
    pi = 3.14159265;
    typedef unsigned long long int LONGINT;
}

Of course there's a main function that is mostly empty and 2 other .cpp files that are working fine. It compiled and worked as wanted before I created the CONSTANTS.cpp and decided to test the same for constants

The problem is that this code tells me that pi in CONSTANTS.cpp isn't defined correctly:

missing type specifier - int assumed. Note: C++ does not support default-int

However, if I add the type to the definition in CONSTANTS

it throws that redefine error.

> e1.obj : error LNK2005: "double CONST::pi" (?pi@CONST@@3NA) already defined in CONSTANTS.obj

> e2.obj : error LNK2005: "double CONST::pi" (?pi@CONST@@3NA) already defined in CONSTANTS.obj

> e3.obj : error LNK2005: "double CONST::pi" (?pi@CONST@@3NA) already defined in CONSTANTS.obj  

That sounds like the declaration in mainHeader.h is the problem but if I change it or take it out I get:

> ...\e3.cpp(34): error C2039: 'pi': is not a member of 'CONST'

> ...\mainHeader.h(12): note: see declaration of 'CONST'

> ...\e3.cpp(34): error C2065: 'pi': undeclared identifier

So I don't understand. If I take it out it doesn't find any definition, if I add it, it finds more than one.


The code for e1.cpp just in case, although I think the only problem would be the first line:

#include "mainHeader.h"
namespace E1 {
    int main()
    {
            int ans = 0;
            for (int i = 0; i < 1000; i++) {
                    if ((i % 3 == 0) || (i % 5 == 0)) ans += i;
            }
            std::cout << ans << std::endl;
            return 0;
    }
}

I tried what I think is every combination of definition and initialization. I used. I tried adding #pragma once to CONSTANTS.cpp, to e1.cpp, I even tried using

#ifndef CONST_H ... #endif

I added #include "mainHeader.h" to each eX.cpp file because there are functions that I want to share between them. Like for example primes.cpp will have some functions that some eX.cpp files will use, so I need to be able to call them.

Any help will be appreciated

laozoka
  • 21
  • 5

1 Answers1

0

Declare pi as const double and initialize it in header, or if you don't want to declare it as const really then prepend extern to declaration. If you don't do this still it will be extern, but it is also defined as 0.