在尝试编译我创建的模板类时,我得到了“左括号匹配之前的文件结尾”。当我双击Visual Studio中的错误消息时,它将我带到“主”文件的顶部,我正在尝试运行代码。当我转到该类的.cpp文件时,所有成员函数都被最小化,除了一个......这让我觉得问题出在哪里?有没有快速找到缺少支具的地方?
我认为它存在于这个特定成员函数中的另一个原因是在声明的底部,因为我正在添加结束括号(我是从一个缩进为5个空格的代码块中添加它们),通常当你输入一个并按下回车键时,Visual Studio将它们置于正确的缩进处,键入一个并按下回车键等,但在这种情况下,它会停止缩进两个“选项卡”,并将继续将它们放在同一个缩进上,我继续输入“}”并按Enter键,输入“}”并按回车键...
成员函数的代码很复杂且足够长,很难通过,即使我有大约5次,但我找不到任何遗漏的地方。这有诀窍吗?我可以在正确的地方寻找吗?谢谢!
编辑:
我没有发布它,因为,说实话,这很丑陋。我正在实现我的第一个真正的类,它是一个模板类 - 链接列表的数组。它太长而且凌乱,我可能应该有一些更好的抽象。除了一点点自我意识之外,我还认为你们会看一下这篇文章并继续前进,我知道我会被诱惑......另外注意:我没有评论所有内容,因为大部分代码都是重复性,仅在不同的初始条件下。
template <typename Type>
void PQueue<Type>::enqueue(Type element)
{
blockT *runner = listHead; //BE CAREFUL - duplicate so as not to eff with listHead - used deep
//case 1: if this is the first element entered
if (listHead == NULL) {
blockT *newBlock = new blockT;
newBlock->blockTArray = new Type[MaxElementsPerBlock];
newBlock->capacity = MaxElementsPerBlock;
newBlock->next = NULL;
newBlock->head = 0;
newBlock->blockTArray[0] = element;
newBlock->tail = 1;
listHead = newBlock;
}
//case 2: element > blockTArray[0]
else if (element >= runner->blockTArray[0]) {
//CASE 2A
if (runner->tail < runner->capacity) {
for (int i = runner->tail; i > 0; i--) { //iterate through array
runner->blockTArray[i] = runner->blockTArray[i-1]; //move everything 1 to right
}
runner->blockTArray[0] = element; //insert "element" at front
runner->tail++; //increment tail
}
//CASE 2B
else {
blockT *newBlock = new blockT;
newBlock->next = runner;
newBlock->blockTArray = new Type[MaxElementsPerBlock];
newBlock->blockTArray[0] = element;
newBlock->tail = 1;
newBlock->head = 0;
newBlock->capacity = MaxElementsPerBlock;
listHead = newBlock;
}
}
//case 3: if runner is less than runner array[head] and array is NOT full
else if (element < runner->blockTArray[0]) {
//TRAVERSE TO FIND END OR BLOCKTARR > ELEMENT
blockT *back;
while (true) {
if (runner->next == NULL || runner->blockTArray[0] <= element) break;
else {
back = runner;
runner = runner->next;
}
}
//EQUAL TO ELEMENT
if (runner->blockTArray[0] == element) {
//INSERT ON THAT ARR IF SPACE
if (runner->tail < runner->capacity) {
for (int i = runner->tail; i > 0; i--) { //iterate through array
runner->blockTArray[i] = runner->blockTArray[i-1]; //move 1 right
}
runner->blockTArray[0] = element; //insert "element" at front
runner->tail++;
}
//ELSE MAKE NEW BLOCK AND PUT 1/2 ELEMENTS ON IT
else {
blockT *newBlock = new blockT;
newBlock->blockTArray = new Type[MaxElementsPerBlock];
newBlock->capacity = MaxElementsPerBlock;
newBlock->blockTArray[0] = element;
newBlock->tail = 1;
newBlock->head = 0;
newBlock->next = runner; //set this new block's "next" = to cell runner was pointing at
back->next = newBlock; //take the cell the runner was stored in (back->next) and set it to new block's address
}
}
//if we stopped because the next arr[0] is smaller, use ->back to place on previous cell
else if (runner->blockTArray[0] < element) {
//if element is bigger than or equal to current arr and -> isn't full, add to front of ->
if (element == back->blockTArray[back->tail - 1] && runner->tail < runner->capacity) {
for (int i = runner->tail; i > 0; i--) { //iterate through array
runner->blockTArray[i] = runner->blockTArray[i-1]; //move everything 1 to right
}
runner->blockTArray[0] = element; //insert "element" at front
runner->tail++;
}
else if (element == back->blockTArray[back->tail - 1] && runner->tail == runner->capacity) {
if (back->tail == back->capacity) {
blockT *newBlock = new blockT;
newBlock->blockTArray = new Type[MaxElementsPerBlock];
newBlock->capacity = MaxElementsPerBlock;
newBlock->blockTArray[0] = element;
newBlock->tail = 1;
newBlock->head = 0;
newBlock->next = runner; //set this new block's "next" = to cell runner was pointing at
back->next = newBlock;
}
else {
for (int i = 0, i < back->tail; i++) {
if (element <= back->blockTArray[i]) {
for (int x = runner->tail; x >= i; x--) { //iterate through array
runner->blockTArray[x] = runner->blockTArray[x-1]; //move everything 1 to right
}
runner->blockTArray[i] = element; //insert "element" at front
}
}
}
}
else if (back->tail == back->capacity) {
for (int i = 0, i < back->tail; i++) {
if (element <= back->blockTArray[i]) {
blockT *newBlock = new blockT;
newBlock->blockTArray = new Type[MaxElementsPerBlock];
newBlock->capacity = MaxElementsPerBlock;
newBlock->head = 0;
for (int x = (MaxElementsPerBlock - (i+3)), z = runner->tail;
x >= 0, z > i; x--, z--) {
newblock->blockTArray[x] = runner->blockTArray[z - 1];
}
runner->blockTArray[i + 1] = element;
runner->tail = i + 2; //you're two ahead in this case since you wrote to i+1
newBlock->tail = i + 1; //because you're one ahead of the element you inserted
back->next = newBlock;
newBlock->next = runner;
break;
}
}
}
}
//NULL CASE if next is null but current arr isn't full, shuffle and insert here
else if (runner->next == NULL && runner->tail < runner->capacity) {
for (int i = 0; i < runner->tail; i++) {
if (element <= runner->blockTArray[i]) {
for (int x = runner->tail - 1; x > blockTArray[i]; x--) {
runner->blockTArray[x + 1] = runner->blockTArray[x];
}
runner->tail++;
}
}
}
else if (runner->next == NULL && runner->tail == runner->capacity) {
for (int i = 0, i < back->tail; i++) {
if (element <= runner->blockTArray[i]) {
blockT *newBlock = new blockT;
newBlock->blockTArray = new Type[MaxElementsPerBlock];
newBlock->capacity = MaxElementsPerBlock;
newBlock->head = 0;
for (int x = (MaxElementsPerBlock - (i+3)), z = runner->tail;
x >= 0, z > i; x--, z--) {
newblock->blockTArray[x] = runner->blockTArray[z - 1];
}
runner->blockTArray[i + 1] = element;
runner->tail = i + 2; //you're two ahead in this case since you wrote to i+1
newBlock->tail = i + 1; //because you're one ahead of the element you inserted
runner->next = newBlock;
newBlock->next = NULL;
break;
}
}
}
}
你可以看到双尾}}。信不信由你花了一段时间思考并写下这段代码。我希望它更干净,更专业,但我不确定最好的方法。抽象?除了股票Visual Studio之外,你们还使用什么吗?任何安装的环境?我刚看了一篇关于“艺术风格2.02”的帖子。值得吗?谢谢你的眼睛这个... ...
答案 0 :(得分:4)
如果您完全不知道自编译以来发生了什么变化,请通过放置
来禁用大部分源代码#if 0
#endif
围绕代码。测试编译以确保错误消失。如果是,请减少注释掉的代码量,然后重试。它应该是一个相当快速的二进制搜索(大型模块的5-10次迭代),用于识别违规括号。