gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:13:57 25 4
gpt4 key购买 nike

我是 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 文件中都定义了这些变量,则“已定义”产生错误。如果我只在一个类中定义它们,“未解决外部符号”错误来自另一个类。

最佳答案

问题出在你的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

关于c++ 全局变量未在此范围内声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43027577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com