Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3806292
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:48:46+00:00 2026-05-19T14:48:46+00:00

I have a Stack container class and I want to create instances of various

  • 0

I have a Stack container class and I want to create instances of various types of Stacks. so I do this:

template <typename T>
class MyStack
{
.
.
   T Pop()
   {
      if(!IsEmpty())
      {
         return data[top--];
      }
      else
      {
         return NULL; // I got error here
      }
 .
 .
}

When I try to use Stack like this:

MyStack<GraphNode> blacks;
GraphNode t = blacks.Pop();

I got this error:

conversion from ‘int’ to non-scalar type ‘GraphNode’ requested

But when I use a pointer type like Stack<GraphNode*> there is no problem. I know that NULL is 0 and I understand why error occurs… What is the elegant way to tell program that there is no data to return without changing the code? should I add something like an implicit type conversion operator to class? how?

NOTE: I’m not using STL

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:48:46+00:00Added an answer on May 19, 2026 at 2:48 pm

    By returning a T by value, the contract of your function is that you will return a value. The two alternatives are to change the contract of your function (e.g. return a T* instead) or to fail to return by throwing an exception.

    Personally, I think that it is acceptable and appropriate to throw an exception in this case. Returning a pointer, or talking a reference to overwrite and returning a boolean success value are both less clean solutions.

    Especially if you provide a public IsEmpty() method there is no reason to choose a less clean solution. Clients that don’t want to handle exceptions can make use of IsEmpty to avoid receiving the exception which becomes the equivalent of an assert.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this strange call stack and I am stumped to understand why. It
I have a stack panel inside of an expander panel that I programaticaly adds
Now I have a stack of free time on my hands, I wanna get
I have read on Stack Overflow some people that have converting to C#2.0 to
So I have a nasty stack overflow I have been trying to track down
Does anyone know if the problems that have been affecting Stack Overflow with regards
I have a web application running on a Gentoo-based LAMP stack. My customers buy
I have thread exception handler which saves the exception stack trace and should close
I have a list of (64-bit) addresses that represent a stack frame, and I
I have a linker command file that assigns the top address of the stack

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.