简体   繁体   English

为什么我在 C++ 的模板中收到所有这些错误?

[英]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<x>::cap'| F:\C
  Programs\TemplateClass.cpp|11|note: declared here| F:\C
  Programs\TemplateClass.cpp||In instantiation of 'void arraylist<x>::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<char>'} and 'int*')| c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\istream|168|note:
  candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]' <near match>| 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;
}

First stop using this:首先停止使用这个:

#include <conio.h>

The only reason you have it is so that you can use:您拥有它的唯一原因是您可以使用:

getch();

You can change that to ask for use input that will have the same affect:您可以更改它以请求具有相同影响的使用输入:

#include <iostream>
...

std::cout << "Please hit return to exit application\n";
std::cin.get();

Please stop using this:请停止使用这个:

using namespace std;

Yes I know have to add std:: in-front of a couple of things but in the long run this will make your life easier.是的,我知道必须在一些事情前面添加std::但从长远来看,这将使您的生活更轻松。


Bugs:错误:

 // You have not specified the type of i
 for (i = 0; i < cap; i++) {

All variables in C++ need a type. C++ 中的所有变量都需要一个类型。 Add a type like this:添加一个这样的类型:

 // Note: There are two places to fix this.
 for (int i = 0; i < cap; i++) {

Your syntax fo accessing members of the array is slightly off:您访问数组成员的语法略有不同:

 cin >> ptr + i;
 ...
 *(ptr) + index = value;

Change these to:将这些更改为:

cin >> ptr[i];
...
ptr[index] = value;

void popdata(int index = cap - 1)
                      ^^^^^^^^^^ this part is not valid
                                 outside the class (where this is being called)
                                 you don't have accesses to the members.

To make it work get rid of that part for now.为了让它工作,暂时摆脱那部分。

void popdata(int index)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM