Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Conflicts with a previous declaration?

I'm working on this assignment, and I keep getting an error for an apparently previously declared method. Here is the .h file:

#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

The error is being triggered at int size();, saying it conflicts with a previous declaration. I know this normally would arise from me including this header file multiple times, but I just don't see where it is happening. These are the include statements for all the files in the project:

#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>

I can see that DListNode.h is technically being included twice, but nowhere is DLList.h included more than once. Am I just making a stupid mistake and not seeing it? (Probably the case). Thanks for any help!

like image 779
Martin Avatar asked Dec 04 '12 01:12

Martin


2 Answers

It is because you have a member variable also named size. You can fix this issue by renaming one of them.

int size;
...
int size();
like image 169
Karthik T Avatar answered Nov 04 '22 05:11

Karthik T


There is a kind of objects in c++ that is callable. It would be confusing when you call DLList::size(), if the variable in your class definition is callable. And that should be the reason why c++ compiler prohibits the declaration for function and variable with the same name.

like image 1
庄子文 Avatar answered Nov 04 '22 06:11

庄子文