小编0x4*_*2D2的帖子

在C++中,关于对象创建,隐式使用了新的吗?

当我创建一个类的对象时,比方说,

class A {
  public: A() {}
};

A a;
Run Code Online (Sandbox Code Playgroud)

只是构造函数被调用?或new隐含使用运算符?

就像我们必须做的那样 A* b = new A();

此外,将在哪里存储ab存储在内存中?堆栈还是堆?

c++ oop

6
推荐指数
2
解决办法
247
查看次数

为什么在字符串中任意放置null终止符会终止它呢?

null字符或null-terminator(\0)用于终止连续的字符序列.我发现在C中,我可以将字符添加到随机位置的字符串中,字符串将从该点切断.例如:

char * s = "Hello\0World";
Run Code Online (Sandbox Code Playgroud)

将导致s等于字符串"Hello".但是,在JavaScript中,情况并非如此:

var s = "Hello\0World";
Run Code Online (Sandbox Code Playgroud)

以上内容无法按预期工作.s将等于字符串"HelloWorld".

为什么这不起作用?

javascript string terminator

6
推荐指数
1
解决办法
6754
查看次数

在 C++ 中读取字节

我正在尝试从二进制文件中读取字节,但没有成功。我尝试了很多解决方案,但没有得到结果。文件结构:

[offset] [type]          [value]          [description] 
0000     32 bit integer  0x00000803(2051) magic number 
0004     32 bit integer  60000            number of images 
0008     32 bit integer  28               number of rows 
0012     32 bit integer  28               number of columns 
0016     unsigned byte   ??               pixel 
0017     unsigned byte   ??               pixel 
........ 
xxxx     unsigned byte   ??               pixel
Run Code Online (Sandbox Code Playgroud)

我是如何尝试的(不起作用):

auto myfile = fopen("t10k-images.idx3-ubyte", "r");
char buf[30];
auto x = fread(buf, 1, sizeof(int), myfile);
Run Code Online (Sandbox Code Playgroud)

c++ binary byte c++11

6
推荐指数
1
解决办法
1万
查看次数

如何在单一设置中合并exe和msi文件?

我正在使用visual studio setup项目创建一个设置,它将创建两个文件,一个是msi,第二个是exe.但我想用它创建单个文件.

.net c#-4.0

6
推荐指数
1
解决办法
3200
查看次数

'std :: string'没有名为'front'的成员

#include <string>
#include <algorithm>
#include <iostream>

int main()
{
    string str;
    string str1;
    int h = 0;
    cin >> str;
    if (str.length() > 10)
    {
        str1 += str.front();
        h = str.length() - 2;
        string s = to_string(h);
        str1 += s;
        str1 += str.back();
        cout << str1;
    }
    else cout << str;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译在XCode中,但不在codeforces.ru/上

 ?an't compile program.cpp:
program.cpp: In function 'int main()':
program.cpp:23:21: error: 'std::string' has no member named 'front'
program.cpp:27:29: error: 'to_string' was not declared in this scope …
Run Code Online (Sandbox Code Playgroud)

c++ string

6
推荐指数
2
解决办法
1万
查看次数

构造函数中的c ++异常安全性

怎么样下面的代码

MyClass a(new Foo(), new Bar());
Run Code Online (Sandbox Code Playgroud)

如果"new Foo()"成功,但"new Bar()"抛出,Foo会泄漏吗?

正在服用

std::unique_ptr<Foo>
Run Code Online (Sandbox Code Playgroud)

要么

std::shared_ptr<Foo>
Run Code Online (Sandbox Code Playgroud)

作为参数,足以防止泄漏?

c++ exception shared-ptr unique-ptr c++11

6
推荐指数
1
解决办法
694
查看次数

为什么被捕后我的异常仍被抛出?

我有以下代码,其中一个变量正在使用函数调用的结果进行初始化.这个函数抛出所以我设置了一个try-catch来捕获异常.由于某种原因,即使在catch子句运行后,异常仍会出现在屏幕上.

#include <iostream>
#include <stdexcept>

int f() { throw std::invalid_argument("threw"); return 50; }

struct S
{
    S()
        try : r(f())
    {
        std::cout << "works";
    }
    catch(const std::invalid_argument&)
    {
        std::cout << "fails";
    }

    int r;
};

int main()
{
    S s;
}
Run Code Online (Sandbox Code Playgroud)

显示异常后,此代码打印"失败":

terminate called after throwing an instance of 'std::invalid_argument'
what():  threw
Run Code Online (Sandbox Code Playgroud)

为什么异常仍然被抛出?我在main中设置了相同的代码,它可以正常工作:

int main()
{
    try { throw std::invalid_argument("blah"); }
    catch(const std::invalid_argument&) { }
}
Run Code Online (Sandbox Code Playgroud)

那么为什么它在初始化列表中使用时会失败呢?

c++ exception-handling c++11

6
推荐指数
1
解决办法
413
查看次数

当未明确使用std :: move时,c ++ 11何时会自动执行?

如果我有一个struct我没有提供任何副本和移动构造函数:

struct MyStruct {
  MyStruct() { // this is the only function
    ...
  }
  ...
};
Run Code Online (Sandbox Code Playgroud)

那么如果我做以下事情:

std::vector<MyStruct> vec;
...
vec.push_back(MyStruct());
Run Code Online (Sandbox Code Playgroud)

而不是使用std::move()如下:

vec.push_back(std::move(MyStruct()));
Run Code Online (Sandbox Code Playgroud)

c ++ 11会巧妙地为我的临时变量做些什么吗?或者,我怎样才能确定它是移动而不是副本?

c++ lvalue rvalue-reference move-semantics c++11

6
推荐指数
3
解决办法
1197
查看次数

clang正在使用错误的系统include目录

我的工作站是RHEL 6.5,它预装了gcc 4.4.7.我们正在做C++ 11工作,因此我们购买了RedHat Developer的Toolset存储库集,其中包括G ++ 4.8.2.这两台G ++版本都安装在这台机器上,但4.8.2是从命令行和构建版本启动时调用的版本:

john@hurricane /home/john/repo/phoenix [PHX-29|?5]  $ which g++
/opt/rh/devtoolset-2/root/usr/bin/g++
john@hurricane /home/john/repo/phoenix [PHX-29|?5]  $ g++ --version
g++ (GCC) 4.8.2 20140120 (Red Hat 4.8.2-15)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)

请注意不寻常的安装位置.

我也从源代码构建了clang并安装了它.但是当我使用clang构建C++ 11项目时,clang正在拾取错误的系统包含.它从gcc 4.4.7中获取系统头,而不是从4.8.2中获取.

G ++配置:

john@hurricane /home/john/repo/phoenix [PHX-29|?5]  $ g++ -E -x c++ - -v < /dev/null 
Using built-in specs. …
Run Code Online (Sandbox Code Playgroud)

c++ linux clang c++11 clang++

6
推荐指数
1
解决办法
3644
查看次数

错误:C++ 要求所有声明都有类型说明符

我是 C++ 新手,我一直在读这本书。我读了几章,想到了自己的想法。我尝试编译下面的代码,但出现以下错误:

||=== 构建:密码调试(编译器:GNU GCC 编译器)===| /Users/Administrator/Desktop/AppCreations/C++/Password/Password/main.cpp|5|错误:C++ 需要所有声明的类型说明符| ||=== 构建失败:1 个错误,0 个警告(0 分钟,2 秒)===|。

我不明白代码有什么问题,有人可以解释一下有什么问题以及如何修复它吗?我读了其他帖子,但我无法理解。

谢谢。

#include <iostream>

using namespace std;

main()
{
    string password;
    cin >> password;
    if (password == "Lieutenant") {
        cout << "Correct!" << endl;
    } else {
        cout << "Wrong!" << endl;
    }

}
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors codeblocks specifier

6
推荐指数
2
解决办法
6万
查看次数