【发布时间】:2017-08-19 01:10:51
【问题描述】:
我是 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
这是我使用这些外部变量的第二个类:
分析.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 中声明我的外部变量是否合适? 文件?
- 如果我在两个 cpp 文件中定义这些变量,“已定义” 产生错误。如果我只在一个类中定义它们,“未解决 外部符号”错误出现在其他类中。
【问题讨论】: