简体   繁体   中英

How to access a variable which is declare in namespace into another cpp file

SpiralTest.h

#ifndef SPIRALTEST_H_
#define SPIRALTEST_H_
namespace eveready
{
 struct TNotes{
  int pie;
  void meth();
 };
 extern TNotes tell;
}
#endif /* SPIRALTEST_H_ */

SpiralTest.cpp

#include "SpiralTest.h"

namespace eveready
{
void TNotes::meth(){
 pie=0;
}
}

now i am trying to access variable pie into abc.cpp

abc.cpp

#include "SpiralTest.h"
using namespace eveready;
tell.meth();

but it shows error when i compile (.text+0x49): undefined reference to `eveready::tell'

i tried also `eveready::tell.meth(); but again it shows same error. what should i do..?

This

extern TNotes tell;

is just a declaration of name tell . You have to define the corresponding object for example in abc.cpp

#include "SpiralTest.h"
using namespace eveready;

//...

TNotes tell;
//..
tell.meth();

Take into account that the function call has to be in some other function. It may not be in a namespace.

You should re-design the program. Spaghetti programming with globals is bad. Instead use object-oriented design (with consistent code formatting):

SpiralTest.h

#ifndef SPIRALTEST_H_
#define SPIRALTEST_H_
namespace eveready
{
  class TNotes
  {
    private:
      int pie;
    public:
      void meth();
  };
}
#endif /* SPIRALTEST_H_ */

SpiralTest.cpp

#include "SpiralTest.h"

namespace eveready
{
  void TNotes::meth()
  {
    pie=0;
  }
}

abc.cpp

#include "SpiralTest.h"
#include "the_file_where_tell_variable_is_allocated.h"

using namespace eveready;

TNotes tell = some_class_in_that_other_file.get();
tell.meth();

structures are deprecated in C++. You are declaring tell as extern in SpiralTest.h which means compiler thinks it would be allocated storage somewhere else. So when it encounters tell in abc.cpp, the linker throws error.

1) Use class instead of structures. 2) define tell (maybe by constructor of TNotes class) in Spiraltest.cpp or abc.cpp

There is no need to extern the instance of your namespace which is not the correct way of accessing the members of namespace. The correct way of doing it would be something like below.

The other thing is trying to initialize the value of pie the same can be done using a constructor of your struct TNotes

Below are the files with changes and which are working as expected.

Note: I have added a definition of meth() for testing my code.

SpiralTest.h

#ifndef SPIRALTEST_H_
#define SPIRALTEST_H_

namespace eveready
{
  struct TNotes
  {
     int pie;
     void meth();
     TNotes(int x)
     {
        pie = x;
     }
  };
}

#endif /* SPIRALTEST_H_ */

SpiralTest.cpp

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

using namespace eveready;


void TNotes::meth()
{
   std::cout<<"inside meth";
}

abc.cpp

#include "SpiralTest.h"

using namespace eveready;

int main()
{
TNotes tell(0);
tell.meth();

return 0;
}

Please feel free to add comments in case of any queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM