草庐IT

system-variable

全部标签

c++ - 变长数组 : How to create a buffer with variable size in C++

我目前正在编写一个移动平均线类。目标是在创建Running_Average类的新对象时能够将缓冲区大小指定为构造函数的一部分。#include#include"Complex.h"#include#include#include#includeusingnamespacestd;classRunning_Average{public:doublesum=0;doubleaverage=0;inti;doubleAverage(void);//MemberfunctionsdeclarationvoidAddSample(double);Running_Average(int);};Ru

C++ : Initializing base class constant static variable with different value in derived class?

我有一个带有常量静态变量a的基类A。我需要类B的实例对静态变量a具有不同的值。这怎么能实现,最好是静态初始化?classA{public:staticconstinta;};constintA::a=1;classB:publicA{//???//Howtoset*a*toavaluespecifictoinstancesofclassB?}; 最佳答案 你不能。所有派生类共享一个静态变量实例。 关于C++:Initializingbaseclassconstantstaticvaria

c++ - 将 System::String 转换为 Const Char *

这个问题在这里已经有了答案:howtoconvertSystem::Stringtoconstchar*?(2个答案)关闭7年前。我正在使用VisualC++2008的GUI创建器制作用户界面。单击按钮时,将调用以下函数。内容应该创建一个文件,并以文本框“文本框”的内容命名该文件,末尾带有“.txt”。但是,这导致我出现转换错误。这是代码:私有(private):System::VoidButton_Click(System::Object^sender,System::EventArgs^e){ofstreammyfile(Textbox->Text+".txt");我的文件.clo

c++ - 函数 system() 如何在 C++ 中工作?

我正在尝试理解使用system("somecommand")在C++中进行的系统调用。这是代码#include#includeusingnamespacestd;intmain(){cout可执行文件“暂停”是从以下代码创建的#includeusingnamespacestd;intmain(){cout我得到以下输出enteranykeytocontinue1HelloWorld有人可以向我解释一下输出吗?我期待这个-Helloenteranykeytocontinue1World 最佳答案 system在shell中运行命令。但

c++ - 错误 : Variable length array of Non-POD element type 'string'

在开始之前,我必须首先声明,我已经研究过针对此错误的可能解决方案。不幸的是,它们都与不使用数组有关,这是我项目的要求。另外,我目前正在学习CS入门类(class),所以我的经验几乎没有。数组的用途是从文件中收集名称。因此,为了初始化数组,我计算了名称的数量并将其用作大小。问题是标题中所述的错误,但我仍然使用一维数组时看不到解决方法。主要.cpp#include#include#include#include#include#include"HomeworkGradeAnalysis.h"usingnamespacestd;intmain(){ifstreaminfile;ofstrea

c++ - std::condition_variable::wait_until 相对于 std::this_thread::sleep_for 有什么优势吗?

在时间等待场景中:oursoftwareworksinthebackground,andsynchronizesdatawiththeserverinevery20-30minutes.我想用std::this_thread::sleep_for但我的上级强烈反对任何形式的sleep功能。他推荐std::condition_variable::wait_until(lock,timeout-time,pred)不知道在这种情况下sleep_for有什么缺点吗? 最佳答案 正如评论中已经指出的那样,这仅取决于您的用例。两者之间的主要区

c++ - std::condition_variable::wait with predicate

在std::condition_variable的文档中,有一个以谓词函数作为参数的wait()重载。该函数将等到谓词函数为真的第一个wake_up。在documentation据说这等同于:while(!pred()){wait(lock);}还有:Thisoverloadmaybeusedtoignorespuriousawakeningswhilewaitingforaspecificconditiontobecometrue.Notethatbeforeentertothismethodlockmustbeacquired,afterwait(lock)exitsitisals

c++ - 为什么 std::chrono::system_clock::to_time_t() 不是 constexpr?

C++标准(github.com/cplusplus/draft)有time_t转换函数(std::chrono::system_clock::to_time_t和std::chrono::system_clock::from_time_t)用于列为static和noexcept但不是constexpr。鉴于time_point和duration上的所有操作基本上都是constexpr(包括duration_cast和time_point_cast),我想不出任何理由来排除它们。在我的本地机器上快速检查libstdc++源代码确认这些函数是作为简单的持续时间/时间点转换实现的。这两个函

c++ - 为什么没有为所有变量报告 "unused variable"警告?

这个问题在这里已经有了答案:g++doesnotshowa'unused'warning(3个答案)关闭8年前。我有这个代码://initializerlists#include#includeintmain(){intvalues[]{1,2,3};std::vectorv{4,5,6};std::vectorcities{"London","NewYork","Paris","Tokio"};return0;}然而,gcc编译器只针对values数组给我unusedvariable警告。为什么v和cities没有被报告?

c++ - 扭曲的逻辑 : a global variable in one file refers to an extern variable but is also referred by that extern variable

文件A.cpp:#includeexternintiA;externintiB=iA;intmain(){std::cout文件B.cppexternintiB;externintiA=2*iB;编译链接运行,out进来debug和release模式是0,0我的问题是它是如何工作的,为什么在链接阶段没有问题?我正在使用VC++2003。 最佳答案 初始化程序覆盖了extern关键字,因此这没有什么“神奇”:您只是在不同的翻译单元中声明和定义两个完全不相关的变量。来自StandardforProgrammingLanguageC++-