const tableStruct = ` | heading | b | c | d | | - | :- | -: | :-: | | cell 1 | cell 2 | 3 | 4 | `
const tableStruct
是一个字符串,它的内容是一个Markdown表格的结构。Markdown表格是一种用于在文本中展示表格的格式,它由行和列组成,每个单元格由竖线|
分隔。表格的第一行是表头,第二行是对齐方式,第三行及以后是表格内容。在表头和对齐方式中,可以使用冒号:
来指定对齐方式,冒号在左边表示左对齐,右边表示右对齐,两边都有表示居中对齐。在表格内容中,每行的单元格数量必须与表头的单元格数量相同,否则会导致格式错误。下面是const tableStruct
所表示的Markdown表格的效果:
heading | b | c | d |
---|---|---|---|
cell 1 | cell 2 | 3 | 4 |
||=== 构建文件: "无目标" 在 "无项目" 中 (编译器: 未知的) ===| D:\Code\C++Files\Mo\test.cpp||In instantiation of 'std::ostream& operator<<(std::ostream&, LinkedList<T>) [with T = int; std::ostream = std::basic_ostream<char>]':| D:\Code\C++Files\Mo\test.cpp|82|required from here| D:\Code\C++Files\Mo\test.cpp|66|error: passing 'const LinkedList<int>' as 'this' argument discards qualifiers [-fpermissive]| D:\Code\C++Files\Mo\test.cpp|46|note: in call to 'int LinkedList<T>::size() [with T = int]'| D:\Code\C++Files\Mo\test.cpp|68|error: passing 'const LinkedList<int>' as 'this' argument discards qualifiers [-fpermissive]| D:\Code\C++Files\Mo\test.cpp|32|note: in call to 'T& LinkedList<T>::operator[](int) [with T = int]'| ||=== 构建 失败: 2 error(s), 2 warning(s) (0 分, 1 秒) ===|
这段代码中出现了两个错误,都是因为operator<<函数中的参数类型为const LinkedList
第一个错误是在调用size函数的时候出现的,因为size函数不是const成员函数,而operator<<函数的参数类型为const LinkedList
第二个错误是在调用operator[]函数的时候出现的,同样是因为operator[]函数不是const成员函数。解决办法是将operator[]函数定义为const成员函数,以便在const对象上也可以调用。
修改后的代码如下:
#include <iostream>
using namespace std;
template <typename T>
class LinkedList {
template <typename U>
friend ostream& operator << (ostream& out, const LinkedList<U>& ll);
struct Node {
T data;
Node* next;
Node(T data = T()) : data(data), next(nullptr) {}
} *head = new Node();
public:
LinkedList() {}
void append(T data) {
Node* ptr = head;
while (ptr -> next != nullptr) ptr = ptr -> next;
ptr -> next = new Node(data);
return;
}
T& operator [] (int index) const {
Node* ptr = head -> next;
int length = 0;
while (length != index) {
ptr = ptr -> next;
length ++;
}
return ptr -> data;
}
int size() const {
Node* ptr = head;
int length = 0;
while (ptr -> next != nullptr) {
ptr = ptr -> next;
length ++;
}
return length;
}
};
template <class T>
ostream& operator << (ostream& out, const LinkedList<T>& ll) {
out << "[ ";
for (int _ = 0; _ < ll.size(); _ ++) {
out << ll[_] << ' ';
}
out << ']';
return out;
}
int main() {
LinkedList<int> l = LinkedList<int>();
l.append(10);
l.append(100);
l.append(1000);
cout << l << endl;
return 0;
}
经过修改后,程序可以正确地输出链表中的元素。
||=== Build: Debug in 02 (compiler: GNU GCC Compiler) ===| D:\for the cpp\02\main.cpp|9|warning: this decimal constant is unsigned only in ISO C90 [enabled by default]| D:\for the cpp\02\main.cpp|10|warning: integer constant is so large that it is unsigned [enabled by default]| D:\for the cpp\02\main.cpp|10|warning: this decimal constant is unsigned only in ISO C90 [enabled by default]| D:\for the cpp\02\main.cpp|11|warning: integer constant is so large that it is unsigned [enabled by default]| D:\for the cpp\02\main.cpp|11|warning: this decimal constant is unsigned only in ISO C90 [enabled by default]| D:\for the cpp\02\main.cpp|19|warning: this decimal constant is unsigned only in ISO C90 [enabled by default]| D:\for the cpp\02\main.cpp|21|warning: this decimal constant is unsigned only in ISO C90 [enabled by default]| ||=== Build finished: 0 error(s), 7 warning(s) (0 minute(s), 0 second(s)) ===|
关于C++编译警告解决方案
在使用GNU GCC Compiler进行C++开发时,可能会遇到多种类型的编译警告。以下是针对提到的具体警告以及ISO C90 unsigned decimal constant large integer warning
的相关解决方案。
1. -Wdeclaration-after-statement
警告
此警告表示在块中的语句之后发现了声明[^1]。这种结构虽然被C++支持并由ISO C99引入,但在默认情况下不被ISO C90标准所接受。如果希望消除此类警告:
方法一: 使用
-std=c99
或更高的标准来编译代码,这会启用对C99特性的支持。g++ -std=c99 your_code.cpp -o output_program
方法二: 将所有变量声明放在代码块的顶部,遵循传统的C89/C90风格编程方式。
int main() { int a; float b; a = 5; // 所有声明都在前面完成后再执行其他操作 b = 3.2f; return 0; }
2. -Wsized-deallocation
警告
当定义了未明确定义的行为释放函数(如 operator delete(void*) noexcept
或者 operator delete[](void*) noexcept
),会出现该警告[^2]。要解决这个问题:
- 确保自定义删除运算符实现符合预期行为,并显式提供这些重载版本。
void operator delete(void* ptr) noexcept { free(ptr); // 假设这里调用了标准库free() } void operator delete[](void* ptr) noexcept { free(ptr); }
3. 处理 ISO C90 unsigned decimal constant large integer warning
对于涉及大整数常量的情况,在ISO C90模式下可能触发关于无符号十进制常量的大数值警告。这是因为早期的标准缺乏足够的类型推导机制处理非常大的字面值。
建议措施:
- 明确指定数据类型修饰符以避免歧义。例如,将大整数标记为长型(
long
)或者更长的形式(unsigned long
,long long
)。const auto bigValue = 1234567890ULL; // ULL 表示 unsigned long long
- 明确指定数据类型修饰符以避免歧义。例如,将大整数标记为长型(
如果确实需要兼容旧版标准,则可以考虑调整预处理器指令或更改目标语言标准至较新的版本,从而减少不必要的约束条件。
总结
通过上述手段能够有效缓解乃至完全移除特定场景下的GCC/G++编译器产生的常见警告信息。合理配置项目构建参数、严格遵守现代编码规范有助于提升软件质量与可维护性。
相关推荐

















