困惑,为什么没有在此范围内声明 c++ 全局变量

Confused, Why 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>

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

using namespace X265_NS;  

// 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

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

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

这是我收到的错误,让我很困惑:

analysis.cpp
1>C:\x265_2.2\x265_2.2\source\encoder\analysis.cpp(170): warning C4244: '=' : conversion from 'int' to 'int8_t', possible loss of data
1>C:\x265_2.2\x265_2.2\source\encoder\analysis.cpp(2814): error C2065: 'numbers' : undeclared identifier
1>C:\x265_2.2\x265_2.2\source\encoder\analysis.cpp(2814): error C2228: left of '.at' must have class/struct/union
1>          type is ''unknown-type''
1>C:\x265_2.2\x265_2.2\source\encoder\analysis.cpp(2814): error C2065: 'frameSize' : undeclared identifier
1>C:\x265_2.2\x265_2.2\source\encoder\analysis.cpp(2814): error C2065: 'frameNumber' : undeclared identifier

yuv.cpp
2>C:\x265_2.2\x265_2.2\source\input\yuv.cpp(219): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
2>x265-static.lib(analysis.obj) : error LNK2005: "int x265::frameNumber" (?frameNumber@x265@@3HA) already defined in yuv.cpp.obj
2>x265-static.lib(analysis.obj) : error LNK2005: "int x265::frameSize" (?frameSize@x265@@3HA) already defined in yuv.cpp.obj
2>     Creating library C:/x265_2.2/x265_2.2/build/vc10-x86/Debug/x265.lib and object C:/x265_2.2/x265_2.2/build/vc10-x86/Debug/x265.exp
2>C:\x265_2.2\x265_2.2\build\vc10-x86\Debug\x265.exe : fatal error LNK1169: one or more multiply defined symbols found

检查 analysis.cpp 看看您是否没有定义

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

再次。

也许你有一个错误的初始化,如:

int frameSize = 0;

而不是:

frameSize = 0;

您可能包含了错误的文件。很可能在某些库源代码中有另一个 yuv.h 文件包含在 analysis.cpp 中,而不是您需要的文件。您可以通过在 yuv.h 中添加一些错误代码并尝试编译 analysis.cpp.

来快速检查这一点