- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在用于测试的主文件中,我从类似于此的代码开始。
//Initialize the data type for the vectors and input variables
if ( choice == 1 )
{
vector<int>MyVector{};
vector<int>NewVect{};
int k1{}, k2{};
}
else if ( choice == 2 )
{
vector<float>MyVector{};
vector<float>NewVect{};
float k1{}, k2{};
}
//Exact Same block for double
while ( true )
{
cout<<": ";
cin>>k1>>k2;
if ((k1 == 0 ) && (k2 == 0)) break;
else
{
MyVector.push_back(k1);
MyVector.push_back(k2);
continue;
}
}
//Insert Sort Algorithm test, Imported from class InsertSort.
//NewVector = sort.sort(MyVector)
return 0;
}
它继续像这样进入另外两个分别声明 float 和 double 的 else if 语句(使用相同的变量名)。然而,编译停止并表示 k1、k2、MyVector 和 NewVector 未在此范围内进一步声明到程序中。我在 main 的“全局”部分声明了它,所以我不太理解为什么声明没有发生。在 if/else if 语句中尝试声明不同类型的相同变量是不可能的吗?
我正在尝试这样做以避免在输入循环中进行额外的测试,这样就可以检查数据类型,定义正确的数据类型并且代码会比原来更短。知道发生了什么吗?
编辑:添加代码。
最佳答案
您不能声明类型取决于运行时条件的变量。变量类型在编译时声明/指定。知道这一点后,您尝试在 if block 内声明不同的类型,但随后每个变量的范围仅限于声明它的 block 。
您正在尝试的可以使用某种多态变量或任何类型的变量来实现,C++17
但不是之前,在 std::any
的名称下。同时,您可以尝试使用 unions
为自己制作类似的东西。下面可以提供一个开始示例来创建您自己的 any
类型,该示例定义了一个包含 int
或 的
:any
双
#include <iostream>
#include <vector>
struct any {
union { int intVal = 0; double dblVal;};
enum {Int = 1, Dbl = 2} type = Int;
any(int val) : intVal(val) {type = Int;}
any(double val) : dblVal(val) {type = Dbl;}
any() {}
};
std::ostream& operator <<(std::ostream& os, const any& x) {
switch(x.type) {
case any::Int : os << x.intVal; break;
case any::Dbl : os << x.dblVal; break;
}
return os;
}
int main()
{
std::vector<any> vect;
any k1, k2;
vect.emplace_back(3);
vect.emplace_back(4);
vect.emplace_back(9.5);
vect.emplace_back(10.5);
for (const auto& i: vect)
std::cout << i << " ";
}
关于c++ - 错误 : Variable Not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42082111/
我有一个模板类 展览.h: template class ExpOf{ ... } 我在整个代码中反复使用,例如T = double [和其他类ExpOf应该一无所知]。 所以我认为一次性编译它是个
如果你有一个名为“Rock”的类,你会做类似的事情:- int main() { Rock; } 为什么会出现“声明未声明任何内容”错误? 它不应该只是调用默认构造函数并在那一刻创建对象吗?
这是一个非常业余的问题,我确信这将是一个非常简单的答案,但我似乎无法弄清楚问题所在。我有一个带有相应 .cpp 文件的头文件,但出于某种原因,每当我尝试使用 g++ 进行编译时,我都会收到错误消息:
我正在使用 MinGW 将我的 Linux 项目转换为在 Windows 上编译。它在 Linux 上编译和运行都很好,但是当我尝试用 MinGW 编译它时,它会出现以下错误消息: camera.h:
我收到“decleration does not declare anything [-fpermissive] 错误”;这是我的代码; #ifndef CAMERA_H #define CAMERA
我正在编写一些 cython 代码,但遇到了一个奇怪的问题。当我尝试将对象作为结构直接从 python 传递到 C 时,cython 生成的代码很好,但 gcc 不喜欢代码输出并给我以下错误:erro
typedef struct BO2Offsets { struct Prestige { u32 offset = 0x000000; char da
我不明白 C++ 中的某些东西,gcc 不喜欢我如何进行。 我做到了: if (!fModeMdi) MyFirstClass* main = (MyFirstClas
在 switch-case 语句中,declaration-with-initialization 是无效的,但允许 declaration-and-then-assignment。如以下代码片段所示
我在我的界面文件中收到一条奇怪的警告。 这也出现在我为此声明属性的那一行。 谁能帮帮我? 最佳答案 在您的项目中的某处,您有一个 #define 将 xOffset 定义为空(除了注释)。像这样: #
declare +x 下面做了什么? (特定于 Bash。)我理解 declare -x,但不理解 declare +x: function the_func { declare +x MY_VA
由于我是 Symfony 的新手,我尝试使用 Doctrine 创建实体关系。我收到错误 “[bundle/entity/file_location”中的属性“report”已经声明,但在我尝试更新架
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 2年前关闭。 Imp
这是给我错误的代码: TAdvSmoothDockItems = class(TCollection) private FOwner: TAdvSmoothDock; FOnChange: T
我对 python 很陌生,我尝试制作一个简单的 GUI 程序。但是,我遇到了一个“问题”,确切地说是一个警告,上面写着:“m”未在全局范围内定义(Python(变量未定义全局))。 我知道如果你想在
当我用 GCC 编译程序时,它会显示“警告:声明未声明任何内容 [-fpermissive]”。 有问题的代码如下: typedef int BOOL; 如何清除警告? 最佳答案 您可以尝试以下操作。
我正在编写一个包含键值对集合的重要类,在编译期间我收到一个我无法弄清楚的非常奇怪的错误。在一个与这里的函数非常相似的函数中,但由于所需代码的复杂性而没有上下文,我收到错误: TValue& opera
这个问题很简单。为了进一步阐明,下面代码中的 Foo1 和 Foo2 在它们的声明方式方面到底有什么区别(例如,使用 class Foo1 { 。 .. }; 而另一个使用 typedef class
我正在开发 Web 项目,并且在从 Oracle 数据库迁移到 mysql 数据库时遇到一些问题。我想用这段代码创建函数: DROP FUNCTION IF EXISTS F_MANIFEST_GAB
是否有一个标志可以传递给 gcc 以禁用此警告?我知道它的作用,但这对我的程序来说无关紧要。 编辑:我只想禁用警告,保持代码不变。编译以下代码会生成警告: struct post{ unsigne
我是一名优秀的程序员,十分优秀!