7 Replies - 705 Views - Last Post: 22 January 2013 - 10:11 PM Rate Topic: -----

#1 Dorden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-January 13

Getting ambiguous symbol errors in a stack of strings

Posted 22 January 2013 - 09:09 PM

I'm trying to make a stack of strings and I'm getting ambiguous symbol errors on lines 22, 30, 43, and 58. The error is coming from the word stack, but I thought I was supposed to use the class name. Can anyone help me understand what is causing this?

#include <iostream>
#include <stack>
#include <string>
using namespace std;

//Define the stack class, set default stack size to 7
//use a template to define type at later point

template<class T,int size=7>
class stack
{
    private: T data[size];
    int stack_ptr;
    public:
    stack(void);
    void push(T x);
    T pop();
	T top();
};
//constructor function to initialize stack and data
template<class T,int size>
stack<T,size>::stack(void)
{
    int i;
    for(i=0;i<size;i++) data[i]=0;
    stack_ptr=0;
}
//Push data onto stack
template<class T,int size>
void stack<T,size>::push(T x)
{
    if(stack_ptr>=size)
    {
        cout<<"cannot push data: stack full"<<endl;
        return;
    }
    data[stack_ptr++]=x;
    cout<<"Pushed\t" << x << "\tonto the stack"<<endl;
    return;
}
//Pop data from stack
template<class T,int size>
T stack<T,size>::pop()
{
    if(stack_ptr<=0)
    {
        cout<<"cannot pop data: stack empty"<<endl;
        return data[0];
    }
    cout<<"popped\t"<< data[--stack_ptr]<< "\tfrom stack"<<endl;
    return data[stack_ptr];
}


int main()
{
    //declaring stack of strings and using default size
    stack<string> c;
	string w;
    string name1 = "Rich";
	string name2 = "Debbie";
	string name3 = "Robin";
	string name4 = "Dustin";
	string name5 = "Philip";
	string name6 = "Jane";
	string name7 = "Joseph";
    c.push(name1);
	c.push(name2);
	c.push(name3);
	c.push(name4);
	c.push(name5);
	c.push(name6);
	c.push(name7);

    //pick up the stack value
    w=c.pop();
	string & i = c.top( );
	string next_top = c.top();

    cout << "The top person the stack is " << next_top << "."<< endl;
   
    return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Getting ambiguous symbol errors in a stack of strings

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Getting ambiguous symbol errors in a stack of strings

Posted 22 January 2013 - 09:22 PM

Well, can you please post the ambiguous error messages that you are getting so that we may be in a better position to help you out.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 Dorden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-January 13

Re: Getting ambiguous symbol errors in a stack of strings

Posted 22 January 2013 - 09:26 PM

Sure, sorry this is my first time going to a forum for help.

1>------ Build started: Project: A-2, Configuration: Debug Win32 ------
1>Build started 1/22/2013 10:01:16 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\A-2.unsuccessfulbuild".
1>ClCompile:
1>  A-2.cpp
1>f:\pf3\a-2\a-2\a-2.cpp(22): error C2872: 'stack' : ambiguous symbol
1>          could be 'f:\pf3\a-2\a-2\a-2.cpp(11) : stack'
1>          or       'h:\program files (x86)\microsoft visual studio 10.0\vc\include\stack(15) : std::stack'
1>f:\pf3\a-2\a-2\a-2.cpp(22): error C2872: 'stack' : ambiguous symbol
1>          could be 'f:\pf3\a-2\a-2\a-2.cpp(11) : stack'
1>          or       'h:\program files (x86)\microsoft visual studio 10.0\vc\include\stack(15) : std::stack'
1>f:\pf3\a-2\a-2\a-2.cpp(30): error C2872: 'stack' : ambiguous symbol
1>          could be 'f:\pf3\a-2\a-2\a-2.cpp(11) : stack'
1>          or       'h:\program files (x86)\microsoft visual studio 10.0\vc\include\stack(15) : std::stack'
1>f:\pf3\a-2\a-2\a-2.cpp(30): error C2872: 'stack' : ambiguous symbol
1>          could be 'f:\pf3\a-2\a-2\a-2.cpp(11) : stack'
1>          or       'h:\program files (x86)\microsoft visual studio 10.0\vc\include\stack(15) : std::stack'
1>f:\pf3\a-2\a-2\a-2.cpp(43): error C2872: 'stack' : ambiguous symbol
1>          could be 'f:\pf3\a-2\a-2\a-2.cpp(11) : stack'
1>          or       'h:\program files (x86)\microsoft visual studio 10.0\vc\include\stack(15) : std::stack'
1>f:\pf3\a-2\a-2\a-2.cpp(43): error C2872: 'stack' : ambiguous symbol
1>          could be 'f:\pf3\a-2\a-2\a-2.cpp(11) : stack'
1>          or       'h:\program files (x86)\microsoft visual studio 10.0\vc\include\stack(15) : std::stack'
1>f:\pf3\a-2\a-2\a-2.cpp(58): error C2872: 'stack' : ambiguous symbol
1>          could be 'f:\pf3\a-2\a-2\a-2.cpp(11) : stack'
1>          or       'h:\program files (x86)\microsoft visual studio 10.0\vc\include\stack(15) : std::stack'
1>f:\pf3\a-2\a-2\a-2.cpp(58): error C2872: 'stack' : ambiguous symbol
1>          could be 'f:\pf3\a-2\a-2\a-2.cpp(11) : stack'
1>          or       'h:\program files (x86)\microsoft visual studio 10.0\vc\include\stack(15) : std::stack'
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.62
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Getting ambiguous symbol errors in a stack of strings

Posted 22 January 2013 - 09:35 PM

I am guessing your variable 'stack' is having a conflict...try changing the name to stack1 and see if it works.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#5 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Getting ambiguous symbol errors in a stack of strings

Posted 22 January 2013 - 09:43 PM

You could try changing "stack" to "cl_stack" or what ever as a class name. It sounds as though your global(externally declared) using statement is causing a namespace clash. You could also try moving the using statement into main() and adding it into any functions requiring the std objects like cin and cout.
Was This Post Helpful? 0
  • +
  • -

#6 Dorden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-January 13

Re: Getting ambiguous symbol errors in a stack of strings

Posted 22 January 2013 - 09:45 PM

Well that took care of the ambiguous symbol errors, but gave me a lot of new ones. I'm trying to see if I can get it to work from here.

1>InitializeBuildStatus:
1>  Touching "Debug\A-2.unsuccessfulbuild".
1>ClCompile:
1>  A-2.cpp
1>f:\pf3\a-2\a-2\a-2.cpp(22): error C2143: syntax error : missing ';' before '<'
1>f:\pf3\a-2\a-2\a-2.cpp(22): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>f:\pf3\a-2\a-2\a-2.cpp(22): error C2988: unrecognizable template declaration/definition
1>f:\pf3\a-2\a-2\a-2.cpp(22): error C2059: syntax error : '<'
1>f:\pf3\a-2\a-2\a-2.cpp(30): error C2039: 'push' : is not a member of '`global namespace''
1>f:\pf3\a-2\a-2\a-2.cpp(31): error C2143: syntax error : missing ';' before '{'
1>f:\pf3\a-2\a-2\a-2.cpp(31): error C2447: '{' : missing function header (old-style formal list?)
1>f:\pf3\a-2\a-2\a-2.cpp(43): error C2143: syntax error : missing ';' before '<'
1>f:\pf3\a-2\a-2\a-2.cpp(43): error C2371: 'stack1' : redefinition; different basic types
1>          f:\pf3\a-2\a-2\a-2.cpp(22) : see declaration of 'stack1'
1>f:\pf3\a-2\a-2\a-2.cpp(43): error C2988: unrecognizable template declaration/definition
1>f:\pf3\a-2\a-2\a-2.cpp(43): error C2059: syntax error : '<'
1>f:\pf3\a-2\a-2\a-2.cpp(43): error C2039: 'pop' : is not a member of '`global namespace''
1>f:\pf3\a-2\a-2\a-2.cpp(56): error C2143: syntax error : missing ';' before '{'
1>f:\pf3\a-2\a-2\a-2.cpp(56): error C2447: '{' : missing function header (old-style formal list?)
1>
1>Build FAILED.

Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,562
  • Joined: 09-June 09

Re: Getting ambiguous symbol errors in a stack of strings

Posted 22 January 2013 - 09:51 PM

stack is already defined as a standard stl container

http://www.cplusplus...ce/stack/stack/

#include <stack>



Why are you including the standard stack container if you are implementing your own?

This post has been edited by jjl: 22 January 2013 - 09:52 PM

Was This Post Helpful? 0
  • +
  • -

#8 Dorden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-January 13

Re: Getting ambiguous symbol errors in a stack of strings

Posted 22 January 2013 - 10:11 PM

I guess I just thought it was needed. I'm still pretty new to templates and classes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1