[英]C++: Conflicts with a previous declaration?
我正在進行這項任務,並且我繼續收到一個明顯先前聲明的方法的錯誤。 這是.h文件:
#ifndef DLLIST_H
#define DLLIST_H
#include <iostream>
#include <fstream>
#include <cstddef>
#include "DListNode.h"
typedef int ItemType;
class DLList
{
private:
friend std::ostream & operator<< (std::ostream & out, DLList & other);
int size;
int * head;
int * tail;
void _copy(const DLList * toCopy);
void _dealloc();
public:
DLList();
DLList(const DLList & toCopy);
~DLList();
DLList & operator=(const DLList & toCopy);
int size();
void append(ItemType item);
void insert(int index, ItemType item);
ItemType pop(int index);
ItemType pop();
const DListNode & operator[] (int idx);
};
std::ostream & operator<< (std::ostream & out, const DLList & d);
#endif
在int size();
觸發錯誤int size();
,說它與先前的聲明沖突。 我知道這通常會由我多次出現,包括這個頭文件,但我只是看不到它發生的地方。 這些是項目中所有文件的include語句:
#include "DLList.h" // DLList.cpp
=======================================
#include "DListNode.h" // DListNode.cpp
=======================================
#include <iostream> // DListNode.h
#include <fstream>
#include <cstddef>
=======================================
#include "DLList.h" // This is the main test file
#include "DListNode.h"
#include <iostream>
我可以看到DListNode.h在技術上被包含兩次,但DLList.h不止一次被包含在內。 我只是犯了一個愚蠢的錯誤而沒有看到它嗎? (可能就是這種情況)。 謝謝你的幫助!
這是因為你有一個名為size的成員變量 。 您可以通過重命名其中一個來解決此問題。
int size;
...
int size();
c ++中有一種可調用的對象。 如果類定義中的變量是可調用的,那么在調用DLList :: size()時會很混亂。 這應該是c ++編譯器禁止聲明具有相同名稱的函數和變量的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.