我不明白为什么可能需要两次,这是我正在阅读的书的引用;
cin.get()语句读取下一个击键,因此该语句会使程序等到按Enter键.(在按Enter键之前没有按键被发送到程序,因此按下另一个键是没有意义的.)如果程序在常规输入后留下未经处理的击键,则需要第二个语句.例如,如果输入一个数字,则键入数字,然后按Enter键.程序读取数字但保持未处理的Enter键,然后由第一个cin.get()读取.
我把它放在源代码中,并没有看到它存在两次.
您键入一些数字并按Enter键结束程序,只有不同的是如果在结束前没有输入则按两次输入.
它的意思是暂停程序,这样做,为什么要使用它两次?
我有一个带有头文件的动态库,如下所示:
#ifndef SRC_H
#define SRC_H
#include<time.h>
namespace test
{
void sleep( int numsec );
}
#endif
Run Code Online (Sandbox Code Playgroud)
和源文件如下:
#include"src.h"
namespace test
{
void sleep(int numsec)
{
sleep(numsec);
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个只包含以下主文件的可执行文件
#include "linked/src.h"
int main()
{
test::sleep(3);
}
Run Code Online (Sandbox Code Playgroud)
我明白了
启动程序:/ usr0/home/gschoenh/Dropbox/Code/cpp/scratch/main/app
[使用libthread_db启用线程调试]
使用主机libthread_db库"/lib/x86_64-linux-gnu/libthread_db.so.1".程序接收信号SIGSEGV,分段故障.
在test :: sleep(numsec = 3)src.cpp中的0x00007ffff7bd863c:8
8 sleep(numsec);
谢谢您的帮助 :)
PS:我花了很多时间试图自己解决这个问题.我已经解决了我自己的最后100个错误.所以请不要像"你^#*$(#($&%(#_.)花费更多的时间来尝试自己做#$&@(#)@."谢谢.
我试图理解为什么unique_ptr有一个nullptr_t构造函数
constexpr unique_ptr::unique_ptr( nullptr_t );
Run Code Online (Sandbox Code Playgroud)
我假设这是因为正常的一个参数构造函数是显式的,因此会拒绝nullptr值:
explicit unique_ptr::unique_ptr( pointer p );
Run Code Online (Sandbox Code Playgroud)
但是当我构建一个例子时,编译器很好:
namespace ThorsAnvil
{
template<typename T>
class SmartPointer
{
public:
SmartPointer() {}
explicit SmartPointer(T*){}
};
}
template<typename T>
using SP = ThorsAnvil::SmartPointer<T>;
int main()
{
SP<int> data1;
SP<int> data2(new int); // fine
SP<int> data3(nullptr); // fine
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
> g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix
> g++ -Wall -Wextra -std=c++11 SP1.cpp
Run Code Online (Sandbox Code Playgroud)
为什么std :: …
重新阅读这个问题:
C++中"new"和"malloc"和"calloc"有什么区别?
我检查了答案,但没有人回答这个问题:
有几个原因(我可以想到两个).
让最好的浮动到顶部.
我的开发环境是[Windows 7; 视觉工作室2010; 86].
我有一个为服务器2003长时间回来构建的DLL.当我在我的项目中使用它并按照新/删除序列使用类时,应用程序在删除调用期间崩溃.即使没有新的和删除之间的任何其他调用我也验证了相同.当我用malloc/free替换new/delete时,没有崩溃.如果我只是声明一个没有new的类的实例,则退出作用域时不会发生崩溃.
什么可能出错?这是我们公司的内部库,所以我无法命名它和其他类似的东西.
附加信息: 要首先使用此库,我必须关闭VS功能"将wchar_t视为内置类型".
代码很简单
{
CLogger * myLog = new CLogger();
delete myLog; // Crash happens here
}
{ // No crash here
CLogger MyLog;
}
{
CLogger * myLog = (CLogger *) malloc (sizeof(CLogger));
free (myLog); // This does not crash.
}
Run Code Online (Sandbox Code Playgroud)
这是专有的库,我不能发布构造函数和析构函数.
是否可以在C++中清除内容(即将EOF设置为开头/重置文件),只知道文件*?我正在写一个带有wb +访问权限的临时文件,并希望有时清除它并截断它而不添加对fclose和fopen的调用.我不认为这是可能的......但如果没有,为什么不呢?
提前致谢!
我不太熟悉C++,特别是IO部分.任何人都可以帮我把以下C++代码翻译成C#吗?
unsigned *PostingOffset, *PostingLength, NumTerms;
void LoadSubIndex(char *subindex) {
FILE *in = fopen(subindex, "rb");
if (in == 0) {
printf("Error opening sub-index file '%s'!\n", subindex);
exit(EXIT_FAILURE);
}
int len=0;
// Array of terms
char **Term;
char *TermList;
fread(&NumTerms, sizeof(unsigned), 1, in);
PostingOffset = (unsigned*)malloc(sizeof(unsigned) * NumTerms);
PostingLength = (unsigned*)malloc(sizeof(unsigned) * NumTerms);
Term = (char**)malloc(sizeof(char*) * NumTerms);
Term = (char**)malloc(sizeof(char*) * NumTerms);
// Offset of each posting
fread(PostingOffset, sizeof(unsigned), NumTerms, in);
// Length of each posting in bytes
fread(PostingLength, sizeof(unsigned), NumTerms, …Run Code Online (Sandbox Code Playgroud) 假设我有一个宠物商店的C++应用程序,它开始于Cats.该应用程序有一个类,它为Cat对话框执行一些数据处理:
CatDlg.cpp:
CatDlg::CatDlg() {//ctor stuff}
CatDlg::onInitDialog() {
DBAccess db;
db.open();
CatRec cat;
cat = db.findRec(m_catID);
CString name = cat.getName();
NameField.SetWindowText(name);
// other catty dialogy stuff
}
Run Code Online (Sandbox Code Playgroud)
现在商店所有者想要将Dogs添加到应用程序中.除数据类型外,Dog数据与Cat数据基本相同.我可以复制CatDlg类,更改几种类型并以这种方式处理它,但这导致维护问题和代码膨胀,99%重复代码:
DogDlg.cpp:
DogDlg::onInitDialog() {
DBAccess db;
db.open();
DogRec dog;
dog = db.findRec(m_dogID);
CString name = dog.getName();
NameField.SetWindowText(name);
// other doggy dialogy stuff
}
Run Code Online (Sandbox Code Playgroud)
或者,我可以通过传入指示动物类型的标志来更改对话框类以处理多个动物,然后相应地处理它.当主人添加新动物时,这也会变得更加丑陋,因为该方法变得更长,有许多重复的代码块:
AnimalDlg.cpp:
AnimalDlg::onInitDialog(AnimalType type) {
DBAccess db;
db.open();
if(type == CAT)
{
CatRec cat;
cat = db.findRec(m_catID);
CString name = cat.getName();
}
else if(type == DOG)
{
DogRec dog;
dog = …Run Code Online (Sandbox Code Playgroud) class CDate
{
// some declarations
public:
CDate& operator ++ ()
{
// increment function;
return *this;
}
};
Run Code Online (Sandbox Code Playgroud)
'&'是否意味着作为回报的参考?
谢谢
SpecC
我的度数角度为-415度,
我把它转换成弧度为
float degreeValue = -415
float radianValue = degreeValue * pi / 180.0;
Run Code Online (Sandbox Code Playgroud)
在这里我得到了-0.7(四舍五入)
如何再次转换为度数以获得相同的角度值(以度为单位).
c++ ×9
c ×2
new-operator ×2
c# ×1
c++11 ×1
class ×1
crash ×1
file ×1
increment ×1
malloc ×1
math ×1
nullptr ×1
oop ×1
reference ×1
translation ×1
unique-ptr ×1
visual-c++ ×1