0

In following code, I think that structure stSameNameButDifferent is local scope definition and so no problem for it. But I got error in run-time. (error : process crash)

Can you explain what's wrong with that code?

test_function.h

#ifndef TEST_FUNC_H_
#define TEST_FUNC_H_
void test_a();
void test_b();

#endif

main.cpp

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

using namespace std;

int main(int argc, const char** argv)
{
        cout << "testing for struct scope" << endl;
        test_a();
        test_b();
        return 0;
}

test_a.cpp

#include <iostream>
#include <sstream>
#include <cstdint>
#include <list>
#include "test_function.h"

struct stSameNameButDifferent
{
        uint32_t nPlayCode;
        uint32_t nGameID;
        std::string sGameName;
};

void test_a()
{
        std::list<stSameNameButDifferent> lstSt;
        for(int i=0; i<10; ++i)
        {
                stSameNameButDifferent st;
                st.nPlayCode = i;
                st.nGameID = 100+i;
                std::ostringstream osBuf;
                osBuf << "Game_" << i;
                st.sGameName = osBuf.str();
                lstSt.push_back(st);
        }
        for(auto &st : lstSt)
        {
                std::cout << st.nPlayCode << ", " << st.nGameID << ", " << st.sGameName << std::endl;
        }
}

test_b.cpp

#include <iostream>
#include <sstream>
#include <cstdint>
#include <list>
#include "test_function.h"

struct stSameNameButDifferent
{
        uint32_t nPlayCode;
        uint32_t nGameID;
        float    fDiscountRate;
        std::string sGameName;
};

void test_b()
{
        std::list<stSameNameButDifferent> lstSt;
        for(int i=0; i<10; ++i)
        {
                stSameNameButDifferent st;
                st.nPlayCode = i;
                st.nGameID = 1000+i;
                st.fDiscountRate = (float)i/100;
                std::ostringstream osBuf;
                osBuf << "Game_" << i;
                st.sGameName = osBuf.str();
                lstSt.push_back(st);
        }
        for(auto &st : lstSt)
        {
                std::cout << st.nPlayCode << ", " << st.nGameID << ", " << st.sGameName << std::endl;
        }
}
5
  • 2
    Congratulation, you have met your first non-trivial sized program, now you know why you shouldn't use using namespace std; and would want define your own namespace. Commented Nov 9, 2015 at 10:53
  • 1
    "I got error" WHAT ERROR?! Commented Nov 9, 2015 at 11:23
  • @Surt: This has nothing to do with using namespace std. Commented Nov 9, 2015 at 11:23
  • @LightnessRacesinOrbit, process crashed. Commented Nov 9, 2015 at 11:35
  • 3
    @heon: And what did your debugger say was the cause? "I got error" and "process crashed" are insufficient problem descriptions. They're not even full sentences. Come on... Commented Nov 9, 2015 at 11:38

2 Answers 2

3

To avoid clashes of the same struct names in multiple translation units, you have to put them in an unnamed namespace like so:

namespace {
    struct stSameNameButDifferent {
        uint32_t nPlayCode;
        uint32_t nGameID;
        std::string sGameName;
    };
}

This will make stSameNameButDifferent only seen privately in the corresponding translation unit (.cpp file).

Otherwise the linker will resolve the symbol with the first one found, hence the errors you see at runtime.

Sign up to request clarification or add additional context in comments.

Comments

0

you have defined stSameNameButDifferent in global scope, so compiler cannot see and analyze both definitions to same struct and it only take the first one it meet and that's why you are getting an error.

You can use two different namespaces for test_a and test_b, so you will not get any error.

1 Comment

"compiler can see two definitions to same struct" You are mistaken.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.