C++ Debug 总结

专治各种疑难杂症。

这函数到底在哪个头文件?

function header
exit #include <stdlib. h>
unlink #include <unistd.h>
mkfifo #include <sys/types.h>
#include <sys/stat.h>

日常报错

include errors detected…

CMakeLists.txt 添加:

include_directories("src")

fexpected a ‘:’

关闭文件重新打开。

incomplete type is not allowed ifstream

#include <fstream>

no suitable conversion function from “std::string” to “char *” exists

#include <iostream>
#include <string>
#include <cstring>
 
int main()
{
    std::string str = "std::string to char*";
 
    char* c = strcpy(new char[str.length() + 1], str.c_str());
    std::cout << c;
 
    delete[] c;
    return 0;
}

does not name a type

交换定义顺序。

argument list for class template “std::vector” is missing

std::vector<string>

no default constructor exists for class “StatementHandler"C/C++(291)

StatementHandler() = default;

expected unqualified-id before ‘)’ token

八成是和某个神秘的宏重名冲突了。

request for member ‘statement_parser’ in ‘* app’, which is of non-class type ‘int’

循环 include

前置申明即可。

invalid use of incomplete type

把函数定义改写到单独的文件里。

error: cannot declare member function … to have static linkage [-fpermissive]

类里面 static,实现里面 不 static,就行。

c++ redefinition of … but same file same line

#pragma once

Multiple definitions of “Main”

删了 build/

file(GLOB_RECURSE my_sources ${PROJECT_SOURCE_DIR}/src/*.cpp)

不要扫描到 CMakeCXXCompilerId.cpp

error: no match for ‘operator<<’ (operand types are ‘std::ostringstream’ {aka ‘std::__cxx11::basic_ostringstream<char>’} and ‘const string’ {aka ‘const std::__cxx11::basic_string<char>’})

template <typename InputIt>
std::string util::join(InputIt begin,
                              InputIt end,
                              const std::string const &separator,
                              const std::string const &concluder)

undefined reference to

如果是模板函数:不能分开定义。

extra qualification on member

void classname::foreach ()

改成

void foreach ()

调用本该派生的方法却是基类的

必须 virtual

free(): invalid size

conversion to inaccessible base class

public 继承

corrupted size vs. prev_size

“std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT=char, _Traits=std::char_traits]” (declared at line 390 of “/usr/include/c++/10/ostream”) is inaccessible

这是因为 std::ostream is not copyable

报错位置:

image-20211005121405975

但其实这里没有任何错误。问题在于此处定义:

image-20211005121430637

解决方法是将其改为指针类型并初始化:

image-20211005121743088