gpt4 book ai didi

c++ - 为什么我会收到 C++ 类错误的重新定义?

转载 作者:行者123 更新时间:2023-11-28 00:14:37 24 4
gpt4 key购买 nike

使用下面的代码,我收到了“SortedLL 的重新定义”错误。所以我从下面的头文件中删除了“class SortedLL”。但是当我从头文件中删除 SortedLL 时,我无法在 main.cpp 文件中声明 SortedLL。

所以我卡住了。我错过了什么?

排序链表头文件

#ifndef SortedLinkedList_SortedLL_h
#define SortedLinkedList_SortedLL_h

struct Node {
int data;
struct Node * next;
};

class SortedLL {
public:
int find(int data);
bool remove(int data);
int size();
bool removeOdd();
bool add(int data);

private:
struct Node * pHead;
int length;
};

#endif

排序链表CPP文件

#include <iostream>

#include "SortedLL.h"

class SortedLL {
private:


// searches the linked list linearly
int findLinear(int data, struct Node ** pPrev, struct Node ** pCurr){
// start at head of list
*pCurr = pHead;
*pPrev = NULL;
int index = 0;
while (*pCurr != NULL){
if ((*pCurr)->data == data){
// found
return index;
}
else {
*pPrev = *pCurr;
*pCurr = (*pCurr)->next;
index++;
}
}

return -1;
}

bool findInsertPointLinear(struct Node ** pPrev, struct Node ** pCurr, int data){
while (*pCurr != NULL){

// if current data is less then new data
if ((*pCurr)->data < data){
// store previous node
*pPrev = *pCurr;

// advance to next position
*pCurr = (*pCurr)->next;
}

// current value is >= new val
else {
// we found insertion point
break;
}
}

return true;

}

// returns the value at the index or NULL if no value exists
bool get(int index, int * val) {

// check if out of bounds
if (index < 0 || index > (length-1)) {
// out of bounds
return false;
}

struct Node * pCurr = pHead;

for (int i=0;i<=index;i++){
pCurr = pCurr->next;
}

if (val != NULL) {
*val = pCurr->data;
}

return true;
}
public:
bool add (int data) {
struct Node * pCurr = pHead;
struct Node * pPrev = NULL;
bool returnVal = true;

// if the list is empty
if (pHead == NULL){
// add the new node
pHead = (struct Node *)malloc(sizeof(struct Node));
pHead->data = data;
pHead->next = NULL;

// return success
length++;
return returnVal;
}

// the list is not empty, so find insertion point
if (findInsertPointLinear(&pPrev, &pCurr, data)){

// if inserting at the begininng of list
if (pPrev == NULL){
pPrev = (struct Node *)malloc(sizeof(struct Node));
if (pPrev != NULL) {
pPrev->data = data;
pPrev->next = pCurr;

// keep track of head pointer
pHead = pPrev;
}
else {
// failed to allocate memory
returnVal = false;
}
}

// if inserting at end of list
else if (pCurr == NULL && pPrev !=NULL) {
pCurr = (struct Node *)malloc(sizeof(struct Node));
if (pCurr != NULL){
pCurr->data = data;
pPrev->next = pCurr;
pCurr->next = NULL;
}
else {
// failure to allocate memory
returnVal = false;
}
}

// if inserting in the middle of list
else {
pPrev->next = (struct Node *) malloc (sizeof(struct Node));
if (pPrev->next != NULL){
pPrev->next->data = data;
pPrev->next->next = pCurr;
}
else {
// failure to allocate memory
returnVal = false;
}
}
}

// increment size of list by 1
if (returnVal == true)
length++;

return returnVal;
}

// returns index of first occurrence of data or -1 if not found
int find(int data){
struct Node * pPrev = NULL;
struct Node * pCurr = NULL;
return findLinear(data, &pPrev, &pCurr);
}








bool remove(int data) {
// try to find data
struct Node * pPrev = NULL;
struct Node * pCurr = NULL;
int removeIndex = findLinear(data, &pPrev, &pCurr);
bool returnVal = false;
// if found
if (removeIndex != -1){
// remove node
return false;
}

// if removing at the head
if (removeIndex == 0){
if (pCurr != NULL){
pHead = pCurr->next;
free(pCurr);
pCurr = NULL;
returnVal = true;
}
else {
printf("trying to remove an element that does not exist\n");
}
}

// if removing at the end
else if (removeIndex == (length-1)){
if (pCurr != NULL) {
free(pCurr);
pCurr = NULL;

if (pPrev != NULL){
pPrev->next = NULL;
}
returnVal = true;

}
else {
printf("trying to remove an element that does not exist\n");

}

}

// removing somewhere in the middle
else {
if (pPrev != NULL && pCurr != NULL){
struct Node * temp = pCurr->next;
free(pCurr);
pCurr = NULL;
pPrev->next = temp;
returnVal = true;
}

// something is wrong
else {
printf("attempting to remove an element from middle but either previous or curr element is NULL\n");
}
}

return returnVal;
}


int size(){
return length;
}

bool removeOdd(){
struct Node * pCurr = pHead;
struct Node * pPrev = NULL;


// iterate over list
while (pCurr != NULL){

// if current data element is odd
if (pCurr->data %2 != 0){
// remove it

// removing at head
if (pCurr == pHead){
pHead = pHead->next;
free(pCurr);
pCurr = pHead;

}

// if removing at the end
else if (pCurr->next == NULL){
free(pCurr);
pCurr = NULL;
pPrev->next = NULL;
}

// removing somewhere in the middle
else {
struct Node * temp = pCurr->next;
free(pCurr);
pPrev->next = temp;

// advance pCurr to next node
pCurr = temp;
}
}

// if current data element even
else {
pPrev = pCurr;
pCurr = pCurr->next;
}
}

return true;
}
}SortedLL;

最佳答案

正如@PaulMcKenzie 指出的那样,您已经在头文件和.cpp 文件中定义了类SortedLL。您在 .cpp 中想要的只是方法定义。像这样的东西:

int SortedLL ::findLinear(int data, struct Node ** pPrev, struct Node ** pCurr) {
...
}

关于c++ - 为什么我会收到 C++ 类错误的重新定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31172927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com