为什么我犯了错误:在我试图编译c++代码时,在Eclipse中初始化参数1::项目(int) [- fper] ?

时间:2021-05-10 20:18:26

I'm new to C++, and have finally given up on trying to get this to compile after staring at it for too long. The compiler seems to be rejecting the constructor prototype in the header file for some reason... I can't figure out what's wrong with it.

我是c++的新手,在盯着它看了太久之后,我终于放弃了让它编译的尝试。由于某种原因,编译器似乎在头文件中拒绝构造函数原型……我弄不明白是怎么回事。

Item.h:

Item.h:

#ifndef ITEM_H_
#define ITEM_H_


class Item {
public:
    Item(int); //This line is what Eclipse keeps flagging up with the error in the title
    virtual ~Item();
    Item* getNextPtr();
    int getValue();
    void setNextPtr(Item *);
};

#endif /* ITEM_H_ */

In my Item.cpp file I have:

在我的项目。cpp文件给我:

int val;
Item* nextPtr = 0;
Item::Item(int value) {
    val = value;
}

Item* Item::getNextPtr() {
    return nextPtr;
}

void Item::setNextPtr(Item *nextItem) {
    nextPtr = nextItem;
} 

int Item::getValue() {
    return val;
}

Item::~Item() {
    // TODO Auto-generated destructor stub
} 

Oops, I'm using GCC. And yeah, they should have been member variables! How do I go about doing that using this format? The code where I use instantiate Item is below. I am aware that there should be no global variables in that either...

哦,我用GCC。是的,它们应该是成员变量!我该如何使用这种格式呢?我使用实例化项的代码如下。我知道,在这个问题上也不应该有全局变量。

#include "LinkList.h"
#include "Item.h"

Item* first = 0;
int length = 0;

LinkList::LinkList(int values[], int size) {
    length = size;
    if (length > 0) {
        Item firstItem = new Item(values[0]);
        Item *prev = &firstItem;
        first = &firstItem;
        for (int i = 0; i < size; i++) {
            Item it = new Item(values[i]);
            prev->setNextPtr(&it);          //set 'next' pointer of previous item to current item
            prev = &it;                     // set the current item as the new previous item
        }

    }
}

LinkList::~LinkList() {
    for (int i = 0; i < length; i++) {
        Item firstItem = *first;
        Item *newFirst = firstItem.getNextPtr();
        delete(first);
        first = newFirst;
    }
}

int LinkList::pop() {
    Item firstItem = *first;
    first = firstItem.getNextPtr();
    return firstItem.getValue();
}

I've just noticed a bug with the functionality of the pop() and destructor functions... please ignore those, I just want to figure out what's wrong with the instantiation of Item.

我刚刚发现了一个带有pop()和析构函数功能的bug……请忽略这些,我只是想知道项目的实例化有什么问题。

GCC error:

GCC错误:

Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\LinkList.o" "..\\src\\LinkList.cpp" 
..\src\LinkList.cpp: In constructor 'LinkList::LinkList(int*, int)':
..\src\LinkList.cpp:16:38: error: invalid conversion from 'Item*' to 'int' [-fpermissive]
..\src\/Item.h:14:2: error:   initializing argument 1 of 'Item::Item(int)' [-fpermissive]
..\src\LinkList.cpp:20:32: error: invalid conversion from 'Item*' to 'int' [-fpermissive]
..\src\/Item.h:14:2: error:   initializing argument 1 of 'Item::Item(int)' [-fpermissive]

21:24:26 Build Finished (took 256ms)

1 个解决方案

#1


4  

Here:

在这里:

Item firstItem = new Item(values[0]);

You are creating a new Item with an item pointer as its argument. This is the same as:

您正在创建一个带有项目指针的新项目作为其参数。这是一样的:

Item firstItem(new Item(values[0]));

And it should be:

它应该是:

Item *firstItem = new Item(values[0]);

#1


4  

Here:

在这里:

Item firstItem = new Item(values[0]);

You are creating a new Item with an item pointer as its argument. This is the same as:

您正在创建一个带有项目指针的新项目作为其参数。这是一样的:

Item firstItem(new Item(values[0]));

And it should be:

它应该是:

Item *firstItem = new Item(values[0]);