我编写了一个C ++代码来测试虚拟方法。但是这个clang编译器+ VS2017会抛出一个异常然后崩溃。 我没有在linux中的CLANG编译器中测试它,但GCC编译器7.3.0工作正常,然后它可以在行中找到错误代码:38“virual~Tuna(){cout<<” Tuna Destructor。“<< endl;}”。 请参阅下面的测试C ++代码:
test.cpp :( CLANG 7.3.0抛出异常,然后在第38行崩溃,但GCC编译器正常工作)
//#include <bits/stdc++.h>
#include <iostream>
using namespace std;
class Fish {
private:
public:
Fish() { cout << "Fish Constructor." << endl; }
Fish(const Fish ©Source) { cout << "Fish Copy Constructor." << endl; }
Fish(Fish &&moveSource) { cout << "Fish Move Constructor." << endl; }
~Fish() { cout << "Fish Destructor." << endl; }
const Fish &operator=(const Fish ©Source)
{
cout << "Fish Assignment Operator=" << endl;
if (this != ©Source) {}
return *this;
}
operator const char *() { return typeid(Fish).name(); }
virtual void Swim() { cout << "Fish swims!" << endl; }
};
class Tuna : public Fish {
private:
public:
Tuna() { cout << "Tuna Constructor." << endl; }
Tuna(const Tuna ©Source) { cout << "Tuna Copy Constructor." << endl; }
Tuna(Tuna &&moveSource) { cout << "Tuna Move Constructor." << endl; }
virual ~Tuna() { cout << "Tuna Destructor." << endl; }
const Tuna &operator=(const Tuna ©Source)
{
cout << "Tuna Assignment Operator=" << endl;
if (this != ©Source) {}
return *this;
}
operator const char *() { return typeid(Tuna).name(); }
void Swim() override { cout << "Tuna swims!" << endl; }
};
class Carp : public Tuna {
private:
public:
Carp() { cout << "Carp Constructor." << endl; }
Carp(const Carp ©Source) { cout << "Carp Copy Constructor." << endl; }
Carp(Carp &&moveSource) { cout << "Carp Move Constructor." << endl; }
~Carp() { cout << "Carp Destructor." << endl; }
const Carp &operator=(const Carp ©Source)
{
cout << "Carp Assignment Operator=" << endl;
if (this != ©Source) {}
return *this;
}
operator const char *() { return typeid(Carp).name(); }
void Swim() override { cout << "Carp swims!" << endl; }
};
void MakeFishSwim(Fish &inputFish)
{
// Calling Fish::Swim()
inputFish.Swim();
}
void DeleteFishMemory(Fish *pFish)
{
if (pFish) {
delete pFish;
pFish = nullptr;
}
}
void DeleteTunaMemory(Tuna *pTuna)
{
if (pTuna) {
delete pTuna;
pTuna = nullptr;
}
}
auto main() -> decltype(0)
{
cout << "Allocating a Carp on the free store:" << endl;
Tuna *pCarp = new Carp;
cout << "Deleting the Carp:" << endl;
// DeleteTunaMemory(pCarp);
DeleteFishMemory(pCarp);
cout << "Instantiating a Carp on the stack:" << endl;
Carp myDinner;
cout << "Automatic desturction as it goes ot of scope:" << endl;
return 0;
}
CLANG 7.0.0将抛出以下错误消息:
clang version 7.0.0 (trunk)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\gcc\..\LLVM\bin
"C:\\gcc\\..\\llvm\\bin\\clang++.exe" -cc1 -triple x86_64-pc-windows-msvc19.11.25506 -emit-obj -mincremental-linker-compatible -disable-free -main-file-name 11_2_inherit.cpp -static-define -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -v -resource-dir "C:\\gcc\\..\\llvm\\lib\\clang\\7.0.0" -internal-isystem "C:\\gcc\\..\\llvm\\lib\\clang\\7.0.0\\include" -internal-isystem "C:\\VS2017\\VC\\Tools\\MSVC\\14.11.25503\\\\ATLMFC\\include" -internal-isystem "C:\\VS2017\\VC\\Tools\\MSVC\\14.11.25503\\\\include" -internal-isystem "C:\\VS2017\\SDK\\10\\include\\10.0.15063.0\\ucrt" -internal-isystem "C:\\VS2017\\SDK\\10\\include\\10.0.15063.0\\shared" -internal-isystem "C:\\VS2017\\SDK\\10\\include\\10.0.15063.0\\um" -internal-isystem "C:\\VS2017\\SDK\\10\\include\\10.0.15063.0\\winrt" -O3 -Wall -Wno-invalid-source-encoding -std=c++17 -fdeprecated-macro -fdebug-compilation-dir "C:\\gcc\\project\\21day\\11\\clang_bug" -ferror-limit 19 -fmessage-length 80 -fmodules -fimplicit-module-maps "-fmodules-cache-path=C:\\Users\\honzheng\\AppData\\Local\\Temp\\org.llvm.clang.honzheng\\ModuleCache" -fno-use-cxa-atexit -fms-extensions -fms-compatibility -fms-compatibility-version=19.11.25506 -fdelayed-template-parsing -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fseh-exceptions -fpack-struct=1 -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -flto-visibility-public-std -o "C:\\Users\\honzheng\\AppData\\Local\\Temp\\11_2_inherit-83936f.o" -x c++ 11_2_inherit.cpp
clang -cc1 version 7.0.0 based upon LLVM 7.0.0-r325576 default target x86_64-pc-windows-msvc
#include "..." search starts here:
#include <...> search starts here:
C:\gcc\..\llvm\lib\clang\7.0.0\include
C:\VS2017\VC\Tools\MSVC\14.11.25503\\ATLMFC\include
C:\VS2017\VC\Tools\MSVC\14.11.25503\\include
C:\VS2017\SDK\10\include\10.0.15063.0\ucrt
C:\VS2017\SDK\10\include\10.0.15063.0\shared
C:\VS2017\SDK\10\include\10.0.15063.0\um
C:\VS2017\SDK\10\include\10.0.15063.0\winrt
End of search list.
11_2_inherit.cpp:38:3: error: unknown type name 'virual'
virual ~Tuna() { cout << "Tuna Destructor." << endl; }
^
Assertion failed: (!needsOverloadResolutionForDestructor() || (data().DeclaredSpecialMembers & SMF_Destructor)) && "this property has not yet been computed by Sema", file C:\src\llvm_package_325576\llvm\tools\clang\include\clang/AST/DeclCXX.h, line 893
Wrote crash dump file "C:\Users\honzheng\AppData\Local\Temp\clang++.exe-7a6ec9.dmp"
0x00007FF7441F2E06 (0x0000000000000001 0x000000F9FE78C5A0 0x000002A000000001 0x00007FFFB1FABF01)
0x00007FFFB200DC17 (0x0000000000000001 0x00007FF700000000 0x0000000000000000 0x000000F9FE78C620), raise() + 0x1E7 bytes(s)
0x00007FFFB200EAA1 (0x0002000000000003 0x000000F900000003 0x00007FFFB20647D0 0x00007FF7473AA9BC), abort() + 0x31 bytes(s)
0x00007FFFB201080A (0x0000000000000000 0x000002A0240F85A8 0x000002A0240F9CC0 0x000000000000037D), _get_wpgmptr() + 0x1C9A bytes(s)
0x00007FFFB2010701 (0x000000000000037D 0x00007FF7473AA9BC 0x000002A0240F85A8 0x000002A0240F9CC0), _get_wpgmptr() + 0x1B91 bytes(s)
0x00007FFFB2010A5F (0x000002A0240F85A8 0x0000000000000000 0x0000000000000000 0x0000000000000000), _wassert() + 0x3F bytes(s)
0x00007FF74540D741 (0x000017758A124773 0x00000000240F8570 0x000002A0240F86E8 0x000002A021587C90)
0x00007FF745415993 (0x00007FF700000000 0x000000F9FE78CD70 0x000000F9FE78CD70 0x000000F9FE78CDB0)
0x00007FF7450DDD57 (0x000002A02151A780 0x000002A02151A768 0x000000F9FE78D1F0 0x00007FF7451C6977)
0x00007FF7450DBA8F (0x0000000000000020 0x000000F9FE78D7A0 0x0000000000000003 0x00007FFFB5817A88)
0x00007FF7450F5606 (0x0000000000000000 0x000002A000000000 0x0000000000000040 0x000002A0213F0CC0)
0x00007FF7450B0187 (0x000002A0240CBA20 0x00007FFFB2010A20 0x0000000000000000 0x000002A02150A060)
0x00007FF7450AFD4C (0x000017758A124C23 0x000017758A125703 0x000002A02151E670 0x0000000000000008)
0x00007FF7450AE935 (0x000002A0214EE8C0 0x00007FF74618C87C 0x000002A02158BF40 0x00007FF7461BF500)
0x00007FF7450AD636 (0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000)
0x00007FF7450AA8B6 (0x0000000000000000 0x0000000000000000 0x000000F9FE78F4E0 0x000002A0214A8A80)
0x00007FF7448EAE10 (0x000002A0214A8A80 0x000002A000000000 0x000002A0214B03C0 0x00007FF74494033F)
0x00007FF7448A741F (0x000002A0214BDE01 0x0000000000000000 0x0000000000000000 0x000002A0214FB8A0)
0x00007FF7449410E9 (0x0000000000000382 0x0000000000003800 0x0000000000000301 0x00007FFFB58180B7)
0x00007FF742907803 (0x000000000000004C 0x000002A002000002 0x000000F9FE78E7C4 0x000000F9FE78E7C0)
0x00007FF7429049E5 (0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000)
0x00007FF7461BB6E8 (0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000)
0x00007FFFB36E8364 (0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000), BaseThreadInitThunk() + 0x14 bytes(s)
0x00007FFFB5847091 (0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000), RtlUserThreadStart() + 0x21 bytes(s)
clang++.exe: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 7.0.0 (trunk)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\gcc\..\LLVM\bin
clang++.exe: note: diagnostic msg: PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script.
clang++.exe: note: diagnostic msg:
********************
PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++.exe: note: diagnostic msg: C:\Users\honzheng\AppData\Local\Temp\11_2_inherit-47fdbe.cpp
clang++.exe: note: diagnostic msg: C:\Users\honzheng\AppData\Local\Temp\11_2_inherit-47fdbe.cache
clang++.exe: note: diagnostic msg: C:\Users\honzheng\AppData\Local\Temp\11_2_inherit-47fdbe.sh
clang++.exe: note: diagnostic msg:
********************
似乎CLANG编译器无法在第38行中识别此错误代码:“virual~Tuna(){cout&lt;&lt;”Tuna Destructor。“&lt;&lt; endl;}”< / strong>即可。但是GCC编译器7.3.0正常工作,可以报告错误行代码并且不会崩溃。
我认为它应该是GCC编译器7.0.0的内部问题。
答案 0 :(得分:1)
首先,您正在运行clang的调试版本,这就是为什么看到堆栈跟踪的原因。运行发行版的clang只会显示一条错误消息,提示您拼写错误的virtual
。请注意,在堆栈跟踪上方,它仍然告诉您错误在于虚拟拼写。 Clang尝试继续编译给出的错误信息,以便您可以获得更多需要修复的错误消息,而不仅仅是一次修复。
简而言之
此外,您可能还希望将Fish析构函数也标记为虚拟,因此请确保将其分派到正确的派生析构函数。