繁体   English   中英

在此范围内未声明c ++全局变量

[英]c++ global variable was not declared in this scope

我是C ++的新手。 我正在尝试修改非常复杂的视频编解码器代码,这是我最后一年学校项目的一部分。 这是我的代码:

这是我在其中声明了三个外部变量的头文件:yuv.h

#include <vector>
namespace X265_NS 
{
extern int frameNumber;
extern int frameSize;
extern std::vector<int>numbers;

class YUVInput : public InputFile, public Thread
{
protected:

// some more variables

public:

// more variables and function declarations

};
}

这是第一个使用以下外部变量的文件:yuv.cpp

#include "yuv.h"
//more includes
#include <vector>

using namespace X265_NS;
int frameNumber;
int frameSize;
std::vector<int>numbers;

// some stuff and function calls
// here I use my extern variables in a function

frameNumber = readCount.get();
frameSize = ceil((double)height / 32) * ceil((double)width / 32);

//more stuff

bool YUVInput::populateFrameQueue()
{
   if(read<1)
             {
                  ifstream file("/home/abu-bakr/bin/test.txt");
                  int number;
                  while (file >> number)
                           numbers.push_back(number);
             }
}

// more stuff

这是我使用这些外部变量的第二类:

analysis.cpp

#include "yuv.h"
#include <vector>
....
using namespace X265_NS;

// some stuff

// its in a function and only place where I am using these variables
int qp_ctu = numbers.at((ctu.m_cuAddr + 1) + (frameSize*(frameNumber - 1)));

// more stuff

我想知道:

  • 在yuv.h文件中声明我的extern变量的正确位置吗?
  • 如果我在两个cpp文件中都定义了这些变量,则会生成“已经定义”错误。 如果仅在一个类中定义它们,则在另一类中出现“未解析的外部符号”错误。

问题出在你的yuv.cpp

using namespace X265_NS;
int frameNumber;
int frameSize;

这些定义是::frameNumber::frameSize ,与X265_NS::frameNumberX265_NS::frameSize

将以上更改为

namespace X265_NS {
    int frameNumber;
    int frameSize;
}

using namespace X265_NS;    // for subsequent code that uses those variables

或者

int X265_NS::frameNumber;
int X265_NS::frameSize;

using namespace X265_NS;    // for subsequent code that uses those variables

暂无
暂无

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

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