当我尝试编译 C++ 代码时,为什么会出现错误:在 Eclipse 中初始化 'Item::Item(int)' [-fpermissive] 的参数 1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14224421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:08:00  来源:igfitidea点击:

Why am I getting error: initializing argument 1 of 'Item::Item(int)' [-fpermissive] in Eclipse when I try to compile my C++ code?

c++compilation

提问by user1804523

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:

项目.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:

在我的 Item.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() 和析构函数功能的一个错误……请忽略这些,我只想弄清楚 Item 的实例化有什么问题。

GCC error:

海湾合作委员会错误:

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)

采纳答案by perreal

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]);