简体   繁体   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?

I'm new to C++, and have finally given up on trying to get this to compile after staring at it for too long. 我是C ++的新手,并且在盯着它看了太久之后终于放弃了尝试编译它。 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. 由于某种原因,编译器似乎拒绝了头文件中的构造函数原型......我无法弄清楚它有什么问题。

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: 在我的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. 哎呀,我正在使用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... 我知道那里应该没有全局变量......

#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: 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)

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

暂无
暂无

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

相关问题 为什么我在尝试编译时遇到此编译错误? - Why am i getting this compile error when i try to compile? 为什么我在下面的 C++ 代码中出现错误:错误:表达式列表在初始化程序中被视为复合表达式 [-fpermissive] - why i am getting error in the below C++ code : error: expression list treated as compound expression in initializer [-fpermissive] 为什么当我尝试在 c++ 中的 Visual Studio 中将字符串分配给 istringstream 对象时出现错误? - Why am i getting error when i try to assign string to istringstream object in visual studio in c++? 当我尝试编译时,为什么我的c ++代码会不断返回“未定义的引用”给我的构造函数之一? - Why does my c++ code keep returning 'undefined reference to' one of my constructors when I try to compile? 我试图在Visual Studio 2010编译c ++代码,我收到链接器错误 - I am trying to compile a c++ code at visual studio 2010 and i am getting a linker error 为什么我的 C++ 代码中出现错误“预期类型名称”错误? - Why am I getting error ''Type name expected' error in my C++ code? 当我尝试在我的 C++ 链表上运行合并排序时,为什么会出现分段错误? (SEGFAULT) - Why am I getting a segmentation fault when I try to run mergesort on my C++ linked list? (SEGFAULT) 为什么在C ++中出现“初始化”:无法从“初始化列表”转换为“ _Objty”的错误? - Why am i getting a “'initializing': cannot convert from 'initializer list' to '_Objty'” error in C++? 为什么我在C ++代码中遇到此错误 - Why I am getting this error in c++ code 为什么我的 C++ 代码中出现“未声明的标识符”错误? - Why am i getting an "undeclared identifier" error in my c++ code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM