jeudi 3 septembre 2020

Why am I getting all these errors in templates in c++?

Here is the list of errors

Errors:
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
F:\C Programs\TemplateClass.cpp|39|error: invalid use of non-static data member 'arraylist::cap'|
F:\C Programs\TemplateClass.cpp|11|note: declared here|
F:\C Programs\TemplateClass.cpp||In instantiation of 'void arraylist::fill() [with x = int]':|
F:\C Programs\TemplateClass.cpp|65|required from here|
F:\C Programs\TemplateClass.cpp|29|error: no match for 'operator>>' (operand types are 'std::istream' {aka 'std::basic_istream'} and 'int*')|
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\istream|168|note: candidate: 'std::basic_istream::__istream_type& std::basic_istream::operator>>(bool&) [with _CharT = char; _Traits = std::char_traits; std::basic_istream::__istream_type = std::basic_istream]' |
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\istream|168|note:   conversion of argument 1 would be ill-formed:|
F:\C Programs\TemplateClass.cpp|29|error: cannot bind non-const lvalue reference of type 'bool&' to an rvalue of type 'bool' 
//code

#include<iostream>
using namespace std;
#include<conio.h>


template <class x>
class arraylist
{
private:
x *ptr;
int cap,I;
 public:

    arraylist(int cap)
    {
        ptr=new x[cap];
    }
    arraylist()//default constructor
    {
    }

    void fill()
     {
    cout<<"Enter "<<cap<<" Elements :-\n";

    for( i=0; i<cap; i++)
    {
        cout<<"Enter "<<i+1<<" Element : ";
        cin>>ptr+i;
        cout<<endl;
    }
    }

void replace(int index,x value) //error message
{
    *(ptr)+index=value;
}

void popdata(int index=cap-1) //error message
{
    delete (ptr+index);
}

void showdata(int index)
{
    cout<<"The data at index "<<index<<" is "<<*(ptr+index);
}

void showlist()
{

    for(i=0; i<cap; i++)
    {
        cout<<ptr+i<<endl;
    }
}
};


int main()
{
arraylist<int>a1;

a1=10;
a1.fill();
a1.showlist();
a1.replace(2,87);
a1.showlist();
a1.popdata(9);
a1.showlist();
getch();



 return 1;
}

Aucun commentaire:

Enregistrer un commentaire