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

  • SEARCH
  • Home
  • 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 7581941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:19:06+00:00 2026-05-30T18:19:06+00:00

when i compile this code #include <iostream> #include<algorithm> using namespace std; template <class T>

  • 0

when i compile this code

#include <iostream>
#include<algorithm>
using namespace std;
template <class T> class IntervalHeap;
template <class T>
class TwoElement
{
    friend  class IntervalHeap <T>;
public:
    T left,
        right;
    };
template<class T>
class IntervalHeap
{
public:
    IntervalHeap(int heapsize=10);
    ~IntervalHeap(){delete[] heap;}
    int size()const { return currentsize;}
    T Min()
    {
         if (currentsize==0)
            // throw OutOfBounds();
          return heap[1].left;
    }
    T Max() {
        if(currentsize==0)
         //throw OutOfBounds();
    return heap[1].right;
    }
    IntervalHeap<T>& Insert(const T& x);
    IntervalHeap<T>& DeleteMin(T& x);
    IntervalHeap<T>& DeleteMax(T& x);

    private:
    int currentsize;//number of elemnts in heap
    int Maxsize;//max elements permited
    TwoElement<T>*heap;//element  array
    };
template<class T>
IntervalHeap<T>::IntervalHeap(int currentsize)
{
    Maxsize=heapsize;
    //determine  number of array positions needed
    //array will be heap[0:n-1];
    int n=Maxsize/2+Maxsize%2+1;
    heap=new TwoElement<T>[n];
    currentsize=0;
    }
template<class T>
IntervalHeap<T>& IntervalHeap<T>::Insert(const T& x)
{
    if (currentsize==Maxsize)
        exit(1);
    if (currentsize<2)
    {
        if(x<heap[1].left)
            heap[1].left=x;
        else heap[1].right=x;
    else
    {
        heap[1].left=x;
        heap[1].right=x;
    }
    curentsize++;
    return *this;


    }
    int lastnode=currentsize/2+currentsize%2;
    bool minHeap;
    if (currentsize%2)
         if (x<heap[lastnode].left)
             minHeap=true;
         else
         {

             lastnode++;
             if (x<=heap[lastnode/2].left)
                 minheap=true;
             else
                 minheap=false;

         }
         if (minHeap) //fix min heap interval heap
         {
             int i=lastnode;
             while (i!=1 && x<heap[i/2].left){
                 heap[i].left=heap[i/2].left;
                 i/=2;



             }
             heap[i].left=x;
             currentsize++;
             if (currentsize%2)
                 heap[lastnode].right=heap[lastnode].left;


         }
         else
         {

             int i=lastnode;
             while(i!=1 &&  x>heap[i/2].right){
                  heap[i].right=heap[i/2].right;
                  i/=2;



             }

             heap[i].right=x;
             currentsize++;
             if (currentsize%2)
                 heap[lastnode].left=heap[lastnode].right;


         }
          return *this;


}
template<class T>
IntervalHeap<T>& IntervalHeap<T>::DeleteMax(T &x)
{
    if (currentsize==0)
        exit(1);
    x=heap[1].right;
    int lastnode=currentsize/2+currentsize%2;
    T y;
    if (currentsize %2)
    {
        y=heap[lastnode].left;
        lastnode--;
    }
    else{

        y=heap[lastnode].right;
        heap[lastnode].right=heap[lastnode].left;


    }
    currentsize--;
    int i=1,ci=2;
    while(ci<lastnode){

        if (ci<lastnode && heap[ci].right<heap[ci+1]) ci++;
         if (y>=heap[ci].right) break;
         //can't put y in  heap[i]
         heap[i].right=heap[ci].right;
         if (y<heap[ci].left)

             ::swap(y,heap[ci].left);
         i=ci;
         ci*=2;
    }

    heap[i].right=y;
    return *this;



}
template<class T>
IntervalHeap<T>& IntervalHeap<T>::DeleteMin(T &x)
{
     if (currentsize==0)
         exit(1);
     x=heap[1].left;
     int lastnode=currentsize/2+currentsize%2;
     T y;
     if (currentsize%2)
     {
         y=heap[lastnode].left;
         lastnode--;
     }
     else
     {
         y=heap[lastnode].right;
         heap[lastnode].right=heap[lastnode].left;

     }

     currentsize--;
     int i=1;
     int ci=2;
     while(ci<=lastnode) //find place for y
     {


          if (ci<lastnode && heap[ci].left>heap[ci+1].left) ci++;
          if (y<=heap[ci].left) break;
          heap[i].left=heap[ci].left;
          if (y>heap[ci].right)
              ::swap(y,heap[ci].right);
          i=ci;
          ci*=2;

     }
     if (i==lastnode && currentsize%2)
         heap[lastnode].left=heap[lastnode].right;
     else 
         heap[i].left=y;

     return *this

}


int main(){


    return 0;
}

there is no errors,but when i add following codes

IntervalHeap<int>Heap;
    Heap.Insert(2);
    Heap.Insert(30);
    Heap.Insert(3);
    Heap.Insert(30);
    Heap.Insert(4);
    Heap.Insert(25);
    Heap.Insert(10);
    Heap.Insert(15);
    Heap.Insert(5);
    Heap.Insert(12);
    Heap.Insert(8);
    Heap.Insert(16);
    Heap.Insert(4);
    Heap.Insert(10);
    Heap.Insert(5);
    Heap.Insert(8);
    Heap.Insert(16);
    Heap.Insert(9);
    Heap.Insert(15);

this errors occur

1>------ Build started: Project: heap_project, Configuration: Debug Win32 ------
1>  heap_project.cpp
1>c:\users\daviti\documents\visual studio 2010\projects\heap_project\heap_project\heap_project.cpp(43): error C2065: 'heapsize' : undeclared identifier
1>          c:\users\daviti\documents\visual studio 2010\projects\heap_project\heap_project\heap_project.cpp(42) : while compiling class template member function 'IntervalHeap<T>::IntervalHeap(int)'
1>          with
1>          [
1>              T=int
1>          ]
1>          c:\users\daviti\documents\visual studio 2010\projects\heap_project\heap_project\heap_project.cpp(214) : see reference to class template instantiation 'IntervalHeap<T>' being compiled
1>          with
1>          [
1>              T=int
1>          ]
1>c:\users\daviti\documents\visual studio 2010\projects\heap_project\heap_project\heap_project.cpp(60): error C2181: illegal else without matching if
1>          c:\users\daviti\documents\visual studio 2010\projects\heap_project\heap_project\heap_project.cpp(52) : while compiling class template member function 'IntervalHeap<T> &IntervalHeap<T>::Insert(const T &)'
1>          with
1>          [
1>              T=int
1>          ]
1>c:\users\daviti\documents\visual studio 2010\projects\heap_project\heap_project\heap_project.cpp(65): error C2065: 'curentsize' : undeclared identifier
1>c:\users\daviti\documents\visual studio 2010\projects\heap_project\heap_project\heap_project.cpp(80): error C2065: 'minheap' : undeclared identifier
1>c:\users\daviti\documents\visual studio 2010\projects\heap_project\heap_project\heap_project.cpp(82): error C2065: 'minheap' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

what is reason?

  • 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-30T18:19:07+00:00Added an answer on May 30, 2026 at 6:19 pm

    This error means that variable heapsize is not declared:

    template<class T>
    IntervalHeap<T>::IntervalHeap(int currentsize)
    {
        Maxsize=heapsize;   // here is the problem
    
        //determine  number of array positions needed
        //array will be heap[0:n-1];
        int n=Maxsize/2+Maxsize%2+1;
        heap=new TwoElement<T>[n];
        currentsize=0;
    }
    

    What is heapsize? Is a Interval<> class attribute?.

    Besides, there is a mistmached braket in IntervalHeap<T>::Insert that doesn’t compile:

    template<class T>
    IntervalHeap<T>& IntervalHeap<T>::Insert(const T& x)
    {
        if (currentsize==Maxsize)
            exit(1);
        if (currentsize<2)
        {
            if(x<heap[1].left)
                heap[1].left=x;
            else heap[1].right=x;
    
        } // this is new bracket
    
        else
        {
            heap[1].left=x;
            heap[1].right=x;
        }
        curentsize++;
        return *this;
        .........
    
    }
    

    An advice: always use brackets, even in single sentences: it makes code clearer.

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

Sidebar

Related Questions

When I compile and run this code: #include <iostream> using namespace std; class Base
Consider the following program: #include <iostream> #include <algorithm> using namespace std; template<class T> struct
This is my code: #include <iostream> #include <fstream> using namespace std; int main() {
EDIT: I realized that this code compiles and works: #include <iostream> template<class Something> class
Why this code does not compile (Cygwin)? #include <vector> template <class Ttile> class Tilemap
I tried following code : #include<iostream> #include<string> using namespace std; string f1(string s) {
#include <boost/algorithm/string.hpp> #include <vector> #include <iostream> #include <string> using namespace std; using namespace boost;
If I compile (gcc 4.6.0) and run this code: #include <iostream> template <typename T>
Please let us consider following code: #include <iostream> using namespace std; union{ int i;
Why is this code resulting in a compiler error? #include <iostream> #include <algorithm> using

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.