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

时间:2017-03-29 21:45:14

标签: c++ scope extern

我是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

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

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

2 个答案:

答案 0 :(得分:3)

您可能包含错误的文件。很可能在某些库源代码中有另一个likwid-topology -g文件,它包含在(byte) (Integer.parseInt("ef",16) & 0xff);中而不是您需要的文件中。您可以通过在yuv.h中添加一些错误代码并尝试编译analysis.cpp来快速检查这一点。

答案 1 :(得分:0)

检查analyze.cpp,看看你是否要定义

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

试。

也许您的初始化错误如下:

int frameSize = 0;

而不是:

frameSize = 0;