简体   繁体   English

如何访问在命名空间中声明的变量到另一个cpp文件中

[英]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 SpiralTest.cpp

#include "SpiralTest.h"

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

now i am trying to access variable pie into abc.cpp 现在我正在尝试将变量pie访问abc.cpp

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' 但它显示错误,当我编译(的.text +×49):未定义的引用`永备::告诉”

i tried also `eveready::tell.meth(); 我也尝试`永备:: tell.meth(); but again it shows same error. 但再次显示相同的错误。 what should i do..? 我该怎么办..?

This 这个

extern TNotes tell;

is just a declaration of name tell . 只是名字tell的声明。 You have to define the corresponding object for example in abc.cpp 您必须在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 SpiralTest.cpp

#include "SpiralTest.h"

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

abc.cpp 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++. C ++不推荐使用结构。 You are declaring tell as extern in SpiralTest.h which means compiler thinks it would be allocated storage somewhere else. 您在SpiralTest.h中将tell声明为extern,这意味着编译器认为它将在其他地方分配存储。 So when it encounters tell in abc.cpp, the linker throws error. 因此,当在abc.cpp中遇到tell时,链接器将引发错误。

1) Use class instead of structures. 1)使用类而不是结构。 2) define tell (maybe by constructor of TNotes class) in Spiraltest.cpp or abc.cpp 2)在Spiraltest.cpp或abc.cpp中定义tell(也许是由TNotes类的构造函数)

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 另一件事是尝试初始化pie的值,这可以使用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. 注意:我已经添加了meth()定义来测试我的代码。

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 SpiralTest.cpp

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

using namespace eveready;


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

abc.cpp 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. 如有任何疑问,请随时添加评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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