Hello. I am creating a template class derived from another template class to basically mimic the behavior of the STL stack for a school project. I am getting a compile time error when try to use a variable named "copy" when testing the overloaded '=' (defined in the base class) for the derived temlate "stack" class. GCC says that the "reference to copy is ambiguous". The program compiles without complaint if I use "cpy" in place of "copy". My professor says that he has never had a problem using the word "copy" before. I have sent him my files and am still waiting to hear back from him about what he thinks the problem is. I have searched my text book, Google, and forums but have not found a solution. Is "copy" used by the language in some way that prohibits its use as a variable name in this situation? Can anyone find a problem with my code that could explain this? Thanks for any help.

The only code in the derived "stack" is for the push() and top() functions, so there should be nothing in that that affects the assignment operator's behavior.

Here is the code for the test program that gives an error:

#include <iostream>
#include "stack.h"

using namespace std;

stack<int> list;
stack<int> copy;
int main(){
  copy = list;
  return 0;
}

Here is the code for the nodes in the linked structure of the stack:

#ifndef NODETYPE_H
#define NODETYPE_H

template <class T>
struct NodeType{
  //Data to be stored in the node
  T item;

  //Pointer to the next node in a list
  NodeType<T> *next;
};

#endif

Here is the code for the overloaded '=' and the function destroy() from the header file for the template base class:

template <class T>
const Base<T>& Base<T>::operator = (const Base<T>& rhs){
   //Check for self-assignment
  if (this != &rhs){
    //Destroy existing data in destination
    destroy();
    
    //Copy nodes if they exist
    if (rhs.count > 0){
      NodeType<T> *temp = rhs.head;
      head = new NodeType<T>;
      head->item = temp->item;
      tail = head;
      temp = temp->next;
      while (temp != NULL){
        tail->next = new NodeType<T>;
        tail = tail->next;
        tail->item = temp->item;
        temp = temp->next;
      }
      tail->next = NULL;
      count = rhs.count;
    }
    else{
      head = NULL;
      tail = NULL;
      count = 0;
    }
  }
}

template <class T>
void Base<T>::destroy(){
   NodeType<T> *temp;
  while (head != NULL){
    temp = head;
    head = head->next;
    delete temp;
  }
  tail = NULL;
  count = 0;
}

Recommended Answers

All 3 Replies

First glance it is

using namespace std;

causes your compiler to find std::copy instead of copy
when it looks at copy

Is "copy" used by the language in some way that prohibits its use as a variable name in this situation

Yes it is, i.e. the std::copy algorithm. It gets pulled in via using namespace std; . So either change the name of the variable, or use your own namespace.

[EDIT]
PS. One more option would be to stop using the using namespace std; altogether and instead specify explicitly everything you use from the std namespace, so ..

#include <iostream>
using std::cout;
using std::cin;

and so on.

commented: Agree with avoiding the using namespace catch-all. +19

Thanks for the help, tetron and mitrmkar. I had a feeling it was going to be something like that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.