小编Moo*_*uck的帖子

为什么这个单独的定义会导致错误?

挑战:

我有这个代码无法编译.你能弄明白什么是错的吗?它给我带来了一次头痛.

// header
namespace values {
  extern std::string address;
  extern int port;
}

// .cpp file
std::string  ::values::address = "192.0.0.1";
int          ::values::port    = 12;
Run Code Online (Sandbox Code Playgroud)

第一眼看上去是正确的.有多少,哪些是错误!?

c++ header declaration extern

8
推荐指数
1
解决办法
457
查看次数

如何创建类的ArrayList?

我有一些类(Car,Motorcycle,Train...等),从延伸class Vehicle.从另一个类我需要创建一个ArrayList类,只能访问那些包含的类ArrayList.

这个概念与此类似,但显然它不起作用;

ArrayList<Class> vehicleType=new ArrayList<Class>();
vehicleType.add(Class.forName("train"));
Run Code Online (Sandbox Code Playgroud)

我该如何解决?谢谢

java class arraylist

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

在C++中没有实现,但仍然可以调用它

我有Simpletron.cpp一个文件,Simpletron.h它声明了一个Simpletron类:

class Simpletron 
{    
public:
    Simpletron();
};
Run Code Online (Sandbox Code Playgroud)

我打电话给Simpletron()我的main.cpp:

#include <iostream>
#include "Simpletron.h"

int main(int argc, char *argv[]) 
{

    Simpletron s();
    std::cin.get();
}
Run Code Online (Sandbox Code Playgroud)

主要功能运行平稳,没有任何警告或错误.这是为什么?如果头文件没有可以链接到的补充,那么如何编译呢?

c++

8
推荐指数
3
解决办法
815
查看次数

删除重载功能.C++ 11.超载的召唤......含糊不清

有全局功能(仅举例):

void func( int i )
{
    std::cout << i + 100 << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我假设用char参数调用这个函数没有任何意义所以我使用delete:

void func(char) = delete;
Run Code Online (Sandbox Code Playgroud)

所以我希望以下呼叫应该是可能的:

func(1);
func(3.1);
func(true);
Run Code Online (Sandbox Code Playgroud)

并且使用char参数调用应该是forbiden:

func('a');
Run Code Online (Sandbox Code Playgroud)

但事实并非如此.打电话时,func('a')我按预期得到:

error: use of deleted function ‘void func(char)’
Run Code Online (Sandbox Code Playgroud)

但在打电话给func(2.3)我的时候:

error: call of overloaded ‘func(double)’ is ambiguous
Run Code Online (Sandbox Code Playgroud)

为什么我会收到此错误?没有删除带有char参数的函数,double被转换为int并调用了func(int),为什么现在它被禁止?

c++ c++11

8
推荐指数
1
解决办法
624
查看次数

模板代码上的编译器堆栈溢出

在我自己的类型擦除迭代器上工作时,我遇到了一个问题,编译器(MSVC10)在此代码上发生了堆栈溢出崩溃:

struct base {};  //In actual code, this is a template struct that holds data
template<class category, class valuetype>  
    struct any;  //In actual code, this is abstract base struct
template<class basetype, class category, class valuetype> 
    struct from; //In actual code, this is function definitions of any

template<class valuetype>
struct any<void,valuetype>
{ void a() {} };
template<class category, class valuetype>  
struct any
    : public any<void,valuetype> //commenting this line makes it compile
{ void b() {} };        

template<class basetype, class valuetype>
struct …
Run Code Online (Sandbox Code Playgroud)

c++ templates visual-c++ visual-c++-2010

7
推荐指数
1
解决办法
831
查看次数

我的二进制文件中的这些额外字节是多少?

我正在用C编写一个小型操作系统.我已经编写了一个bootloader,我现在正在尝试使用gcc以下代码编译一个简单的C文件("内核"):

int main(void) { return 0; }
Run Code Online (Sandbox Code Playgroud)

我使用以下命令编译该文件:

gcc kernel.c -o kernel.o -nostdlib -nostartfiles
Run Code Online (Sandbox Code Playgroud)

我使用链接器使用此命令创建最终图像:

ld kernel.o -o kernel.bin -T linker.ld --oformat=binary
Run Code Online (Sandbox Code Playgroud)

linker.ld文件的内容如下:

SECTIONS
{
    . = 0x7e00;

    .text ALIGN (0x00) :
    {
        *(.text)
    }
}

(引导加载程序将图像加载到地址0x7e00.)

这似乎工作得很好 - ld产生一个128字节的文件,包含前11个字节中的以下指令:

00000000 55                             push    ebp
00000001 48                             dec eax
00000002 89 E5                          mov ebp, esp
00000004 B8 00 00 00 00                 mov eax, 0x00000000
00000009 5D                             pop ebp
0000000A C3                             ret

但是,我无法弄清楚其他117个字节的用途.拆解它们似乎会产生一堆没有任何意义的垃圾.附加字节的存在让我想知道我是否做错了什么.

我应该担心吗?

hexdump文件

c gcc operating-system kernel

7
推荐指数
1
解决办法
418
查看次数

我无法理解c ++中的结构声明

在C中,每当我们想要声明或定义结构时,我们必须使用结构前缀.但是,一旦结构成为c ++中的一类,事情就会发生变化.我们struct在声明结构时不再需要使用前缀.在这种情况下,我猜结构标签C成为了一个类型的名称C++.

但是,这并不意味着我们不能使用struct前缀.我们仍然可以使用struct前缀.例如,c ++的创建者Bjarne Stroustrup介绍了一个声明带有和不带struct前缀的结构的例子,这让我感到困惑.

下面是尝试使用模板参数T创建结构的结构定义.这些编译正常,没有错误.

template<class T> struct linked_list {
    T element;
    linked_list<T> *next;
};
template<class T> struct linked_list {
    T element;
    struct linked_list<T> *next;
};
Run Code Online (Sandbox Code Playgroud)

现在,下面是函数声明,其返回类型和参数类型是结构.即使这些与上面没有什么不同,第一个从下面的两个函数声明,一个带结构前缀,给我一个错误的Visual Studio c ++ 2012

template<class T> struct linked_list<T> *add_list(T element, struct linked_list<T> *tail);
template<class T> linked_list<T> *add_list(T element, linked_list<T> *tail);
Run Code Online (Sandbox Code Playgroud)

我真的不明白事情是如何运作的.我不明白这些声明之间的区别.谁能给我一个详细的解释?

c c++ struct structure class

7
推荐指数
1
解决办法
711
查看次数

继承自专门的类?

这是有效的 C++ 吗?

template<class category>
class any_iterator : public any_iterator<void>
{ 
public:
        typedef any_iterator<void> any_iter_void;

        any_iterator() : any_iter_void() {}
};
template<>
class any_iterator<void>
{ 
public:
        typedef any_iterator<void> any_iter_void;

        any_iterator() {}
        void foo() {};
};

int main() {
    any_iterator<int> a;
    a.foo();
}
Run Code Online (Sandbox Code Playgroud)

MSVC10 在没有错误/警告的情况下接受它\WALL,但gcc-4.5.1抱怨:

prog.cpp:3:5: 错误:不完整类型“class any_iterator”的无效使用
prog.cpp:2:11: 错误:“class any_iterator”
prog.cpp 的声明:在函数“int main()”中:
prog。 cpp:21:11: 错误: 'class any_iterator' 没有名为 'foo' 的成员
prog.cpp: 在构造函数 'any_iterator::any_iterator() [with category = int]':
prog.cpp:20:27: 实例化自这里
prog.cpp:7:44: 错误:类型“any_iterator”不是“any_iterator”的直接基础

有人可以引用标准显示是否应该或不应该编译吗?我认为这是 MSVC 中的一个错误。

作为说明,我知道正确的做法是声明类,专门化根,然后 …

c++ inheritance templates template-specialization

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

我可以用C++编写基于以太网的网络程序吗?

我想编写一个程序并在两台机器上运行它,并在以太网帧中将一些数据从一台机器发送到另一台机器.

通常,应用程序数据位于OSI模型的第7层,有类似于内核限制或API限制的东西,这会阻止我编写一个程序,在该程序中我可以指定目标MAC地址并将一些数据发送到该MAC作为以太网负载?然后写一个程序来监听传入帧和从指定的源MAC地址抓取的帧,从帧中提取数据的有效载荷?

(所以我不想要任何其他开销,如IP或TCP/UDP标头,我不想高于第2层).

这可以用C++完成,还是必须在IP层进行所有通信,这可以在Ubuntu上完成吗?特别喜欢指点或提供示例!:d

我的问题显然是我对c ++网络编程的新手,据我所知,如果我想通过网络进行通信,我必须使用一个socket()在IP层工作的呼叫或类似的,所以我可以写一个c ++程序在OSI第2层工作,是否有API,Linux内核是否允许这样做?

c++ sockets linux ethernet

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

在C++中抛出异常后应该如何释放内存?

如果这个问题是重复的,我很抱歉 - 我搜索了一段时间,但是我的Google-fu可能不符合要求.

我正在修改一个调用C库的C++程序.C库分配一堆内存(使用malloc()),C++程序使用它然后释放它.问题是C++程序可以在执行过程中抛出异常,从而导致分配的内存永远不会被释放.

作为一个(相当做作的)例子:

/* old_library.c */
char *allocate_lots() {
    char *mem = (char *)malloc(1024);
    return mem;
}

/* my_prog.cpp */
void my_class::my_func () {
    char *mem = allocate_lots();
    bool problem = use(mem);
    if (problem)
        throw my_exception("Oh noes! This will be caught higher up");
    free(mem);  // Never gets called if problem is true
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:我应该怎么处理这个问题?我的第一个想法是将整个事物包装在一个try/catch块中,并且在catch中只需检查并释放内存并重新抛出异常,但这对我来说似乎没有优点和笨重(如果我不顺利的话)想要真正捕获异常).有没有更好的方法呢?

编辑:我可能应该提到我们正在使用g ++ 4.2.2,从2007年开始使用std :: unique_ptr之前.将它归结为企业惯性.

memory malloc exception c++03

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