小编soc*_*cks的帖子

从makefile创建两个单独的可执行文件(g ++)

目前,我将makefile设置为编译并制作一个相当大的项目.我编写了第二个带有main函数的cpp文件来运行测试.我希望这些单独运行,但是一起构建并且它们使用相同的文件.这是如何完成的?

编辑:作为参考,这是我当前的makefile.我不确定如何调整它.

CC=g++
CFLAGS=-c -Wall -DDEBUG -g
LDFLAGS=
SOURCES=main.cpp Foo.cpp Bar.cpp Test.cpp A.cpp B.cpp C.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=myprogram

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $< -o $@
Run Code Online (Sandbox Code Playgroud)

c++ makefile g++

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

返回带有重载运算符的副本

我有一个类Foo,我已经重载了+运算符,如下所示:

Foo Foo::operator+(const Bar &b)
{
    Foo copy = (*this);
    if (someCondition) return copy;
    //snip
}
Run Code Online (Sandbox Code Playgroud)

对我来说,这看起来很合理.但是,每当我返回副本时,Visual Studio都会警告我"可能是由于堆损坏"而导致的错误.我做了什么有什么问题吗?

编辑:更新更多信息.

错误消息:

Windows已在sample.exe中触发了断点.

这可能是由于堆的损坏,这表明sample.exe或它已加载的任何DLL中的错误.

这也可能是由于用户在sample.exe具有焦点时按下F12.

输出窗口可能包含更多诊断信息.

复制构造函数:

Foo::Foo(const Foo&p)
{
    some_pointer = p.get_some_pointer();
    some_value = p.get_some_value();
}
Run Code Online (Sandbox Code Playgroud)

它打破的代码:

//within dbgheap.c
    extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
            const void * pUserData
            )
    {
            if (!pUserData)
                return FALSE;

            if (!_CrtIsValidPointer(pHdr(pUserData), sizeof(_CrtMemBlockHeader), FALSE))
                return FALSE;

            return HeapValidate( _crtheap, 0, pHdr(pUserData) );
    }
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading

5
推荐指数
1
解决办法
739
查看次数

标签 统计

c++ ×2

g++ ×1

makefile ×1

operator-overloading ×1