繁体   English   中英

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

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

我是C ++的新手,并且在盯着它看了太久之后终于放弃了尝试编译它。 由于某种原因,编译器似乎拒绝了头文件中的构造函数原型......我无法弄清楚它有什么问题。

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_ */

在我的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
} 

哎呀,我正在使用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();
}

我刚刚注意到pop()和析构函数功能的一个错误...请忽略它们,我只是想弄清楚Item的实例化有什么问题。

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)

这里:

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

您正在创建一个新项目,其中项目指针作为其参数。 这与:

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

它应该是:

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

暂无
暂无

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

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