【发布时间】:2021-11-25 07:12:25
【问题描述】:
我不确定导致它无法编译的大部分问题来自哪里。不过,我对此相对较新,所以很明显我哪里出错了。这部分项目模拟将卡片信息放入链接列表,但让代码正常工作是我可以自己做的事情。我只是想不通为什么我不能让它与makefile一起编译,也不知道“头未在此范围内声明”之类的错误来自哪里。
编辑以使用最近的更改更新代码并添加编译器错误
proj3.cpp
#include<iostream>
#include <fstream>
#include "tsllist.h"
#include "card.h"
using namespace std;
int main()
{
//Declaration of Variables
TSLList<Card> myList;
int elementSuit;
int elementFace;
char elementThree;
ifstream inFile;
inFile.open ("cards.txt");
inFile >> elementSuit; //Read in 1st element
inFile >> elementFace; //Read in 2nd elemen
Card myCard(elementFace, elementSuit);
inFile >> elementThree; //Read in 3rd element
while(!inFile.eof()){
if (inFile.eof()) break;
switch(elementThree){
case 'a' :
myList.insertInOrder(myCard);
myList.printAll();
break;
case 'd' :
myList.deleteVal(myCard);
myList.printAll();
break;
case 'D':
myList.deleteAllVal(myCard);
myList.printAll();
break;
default:
break;
}
inFile >> elementSuit; //Read in 1st element
inFile >> elementFace; //Read in 2nd elemen
inFile >> elementThree; //Read in 3rd element
Card myCard(elementFace, elementSuit);
}
inFile.close();
return 0;
}
tsllist.h
#ifndef TS_LINKED_LIST_H
#define TS_LINKED_LIST_H
#include<iostream>
using namespace std;
template<class T>
class TSLList {
public:
// Constructor
TSLList() {
head = nullptr;
}
//D Destructor
~TSLList() {
//clearList();
TSLLNode *tmp = head;
while(tmp != nullptr)
{
//will delete each node pointed to by head
//and prints when doing so
head = head->next;
tmp->next = nullptr;
cout << "Deleting " << tmp << endl;
delete tmp;
tmp = head;
}
}
// prints the info content and address of each node in the list
void printAll() const {
for (TSLLNode *tmp = head; tmp != nullptr; tmp = tmp->next)
std::cout << "->[" << tmp->info << "," << tmp << "]";
std::cout << std::endl;
}
// Inserts node in ascending order
void insertInOrder(T val) {
TSLLNode *prev; // for previous node when parsing
TSLLNode *tmp; //pointer for current node when parsing list
TSLLNode *newNode = new TSLLNode;
newNode->info = val;
newNode->next = nullptr;
if(head==nullptr)
{
head = newNode;
}
else if(newNode->info <= head->info) //
{
newNode->next = head;
head = newNode;
}
else
{
tmp = head;
prev = nullptr;
while(tmp != nullptr && tmp->info < newNode->info)
{
prev = tmp;
tmp = tmp->next;
}
prev->next = newNode;
newNode->next = tmp;
}
// Deletes an occurrence of argument
template <class T> T deleteVal(T val) {
TSLLNode *tmp;
TSLLNode *prev;
if(!head)
return val;
if(head->info == val)
{
tmp=head->next;
delete head;
head = tmp;
return val;
}
else
{
tmp = head;
while(tmp != nullptr && tmp->info != val)
{
prev = tmp;
tmp = tmp->next;
}
if(tmp)
{
prev->next = tmp->next;
delete tmp;
return val;
}
}
}
// Deletes all occurrences of argument
void deleteAllVal(T val) {
TSLLNode *tmp=head;
TSLLNode *prev=head;
while(tmp!=nullptr)
{
if(tmp->info == val)
{
if(tmp==head)
{
head = tmp->next;
delete tmp;
tmp = head;
}
else
{
prev->next = tmp->next;
delete tmp;
tmp = prev->next;
}
}
else
{
prev = tmp;
tmp = tmp->next;
}
}
return;
}
// Clears the list (deallocates memory)
//void TSLList::clearList() {
//TSLLNode *tmp = head;
// while(tmp != nullptr)
// {
//will delete each node pointed to by head
//and prints when doing so
// head = head->next;
// tmp->next = nullptr;
// std::cout << "Deleting " << tmp << std::endl;
// delete tmp;
// tmp = head;
// }
// }
private:
//Node stored in linked list
struct TSLLNode {
TSLLNode(T el = T()) {
info = el;
next = nullptr;
}
int info;
TSLLNode *next;
};
TSLLNode *head; // head of the list
};
#endif
制作文件
## compiler
CXX = g++ -std=c++11
CXXFLAGS = -pedantic -g
proj3: proj3.o card.o
$(CXX) $(CXXFLAGS) proj3.o card.o -o proj3
NumberList.o: card.cpp card.h
$(CXX) $(CXXFLAGS) -c card.cpp
proj3.o: proj3.cpp card.h
$(CXX) $(CXXFLAGS) -c proj3.cpp
clean:
$(RM) proj3 *.o
makefile/编译器错误
g++ -std=c++11 -pedantic -g -c proj3.cpp
In file included from proj3.cpp:9:0:
card.h:5:12: error: using-declaration for non-member at class scope
using std::ostream;
^~~~~~~
proj3.cpp:11:7: error: expected nested-name-specifier before ‘namespace’
using namespace std;
^~~~~~~~~
proj3.cpp:54:1: error: expected ‘}’ at end of input
}
^
In file included from proj3.cpp:8:0:
tsllist.h: In constructor ‘TSLList<T>::TSLList()’:
tsllist.h:22:4: error: ‘head’ was not declared in this scope
head = nullptr;
^~~~
tsllist.h: In destructor ‘TSLList<T>::~TSLList()’:
tsllist.h:28:8: error: ‘TSLLNode’ was not declared in this scope
TSLLNode *tmp = head;
^~~~~~~~
tsllist.h:28:18: error: ‘tmp’ was not declared in this scope
TSLLNode *tmp = head;
^~~
tsllist.h:28:24: error: ‘head’ was not declared in this scope
TSLLNode *tmp = head;
^~~~
tsllist.h: In member function ‘void TSLList<T>::printAll() const’:
tsllist.h:43:9: error: ‘TSLLNode’ was not declared in this scope
for (TSLLNode *tmp = head; tmp != nullptr; tmp = tmp->next)
^~~~~~~~
tsllist.h:43:19: error: ‘tmp’ was not declared in this scope
for (TSLLNode *tmp = head; tmp != nullptr; tmp = tmp->next)
^~~
tsllist.h:43:25: error: ‘head’ was not declared in this scope
for (TSLLNode *tmp = head; tmp != nullptr; tmp = tmp->next)
^~~~
tsllist.h: In member function ‘void TSLList<T>::insertInOrder(T)’:
tsllist.h:50:4: error: ‘TSLLNode’ was not declared in this scope
TSLLNode *prev; // for previous node when parsing
^~~~~~~~
tsllist.h:51:14: error: ‘tmp’ was not declared in this scope
TSLLNode *tmp; //pointer for current node when parsing list
^~~
tsllist.h:52:14: error: ‘newNode’ was not declared in this scope
TSLLNode *newNode = new TSLLNode;
^~~~~~~
tsllist.h:52:28: error: ‘TSLLNode’ does not name a type
TSLLNode *newNode = new TSLLNode;
^~~~~~~~
tsllist.h:56:7: error: ‘head’ was not declared in this scope
if(head==nullptr)
^~~~
tsllist.h:74:9: error: overloaded function with no contextual type information
prev->next = newNode;
^~
tsllist.h:74:9: error: base operand of ‘->’ is not a pointer
tsllist.h:79:3: error: a template declaration cannot appear at block scope
template <class T> T deleteVal(T val) {
^~~~~~~~
tsllist.h:151:2: error: expected primary-expression before ‘private’
private:
^~~~~~~
tsllist.h:162:27: error: expected primary-expression before ‘>’ token
TSLLNode<T> *head; // head of the list
^
tsllist.h:162:30: error: ‘head’ was not declared in this scope
TSLLNode<T> *head; // head of the list
^~~~
proj3.cpp: At global scope:
proj3.cpp:54:1: error: expected ‘;’ at end of input
}
^
makefile:12: recipe for target 'proj3.o' failed
make: *** [proj3.o] Error 1
card.h
#ifndef CARD_H
#define CARD_H
#include <iostream>
using std::ostream;
// Enumerated type that represents the card suits
enum suit {diamonds, clubs, hearts, spades, joker};
class Card
{
public:
//default constructor - creates Joker card by calling 2-parameter constructor
Card() : Card(-1, joker) {};
//constructor that takes a card's face value (an integer) and its suit
// card face values: Ace=0, 2=1, 3=2, ... Q=11, K=12
Card (int, suit);
// compare and return true if face value of *this is less than that of cd, false otherwise
bool operator<(const Card& cd) const;
// compare and return true if face value of *this is greater than that of cd, false otherwise
bool operator>(const Card& cd) const;
// compare and return true if face value of *this is less than or equal to that of cd, false otherwise
bool operator<=(const Card& cd) const;
// compare and return true if face value of *this is greater than or equal to that of cd, false otherwise
bool operator>=(const Card& cd) const;
// compare and return true if *this has the same face value as cd, false otherwise
bool operator==(const Card& cd) const;
// compare and return true if *this has the a different face value than cd, false otherwise
bool operator!=(const Card& cd) const;
// declare ostream << a friend of this class and overload << operator to display card
friend ostream& operator << (ostream& os, const Card& cd)
{
switch (cd.cardFace)
{
case 10:
os <<"J";
break;
case 11:
os <<"Q";
break;
case 12:
os <<"K" ;
break;
case 0:
os <<"A" ;
break;
case -1:
os <<"j" ;
break;
default:
os << cd.cardFace + 1;
}
switch (cd.cardSuit)
{
case 0:
os << "D";
break;
case 1 :
os << "C";
break;
case 2:
os << "H";
break;
case 3:
os << "S";
break;
case 4:
os << "*";
break;
}
os << "[" << cd.pointValue << "]";
return os;
}
private:
suit cardSuit; // card's suit
int cardFace; // card's face value
int pointValue; // card's point value (from its face)
};
#endif
card.cpp
#include<iostream>
#include"card.h"
using namespace std;
//constructor that takes a card's face value (an integer) and its suit
// card face values: Ace=0, 2=1, 3=2, ... Q=11, K=12
Card::Card (int x, suit y)
{
cardFace = x;
cardSuit = y;
}
//compare and return true if face value of *this is less than that of cd, false otherwise
bool Card::operator<(const Card& cd) const
{
return cardFace<cd.cardFace;
}
// compare and return true if face value of *this is greater than that of cd, false otherwise
bool Card::operator>(const Card& cd) const
{
return cardFace>cd.cardFace;
}
// compare and return true if face value of *this is less than or equal to that of cd, false otherwise
bool Card::operator<=(const Card& cd) const
{
return cardFace<=cd.cardFace;
}
// compare and return true if face value of *this is greater than or equal to that of cd, false otherwise
bool Card::operator>=(const Card& cd) const
{
return cardFace>=cd.cardFace;
}
// compare and return true if *this has the same face value as cd, false otherwise
bool Card::operator==(const Card& cd) const
{
return cardFace==cd.cardFace;
}
// compare and return true if *this has the a different face value than cd, false otherwise
bool Card::operator!=(const Card& cd) const
{
return cardFace!=cd.cardFace;
}
【问题讨论】:
-
如果您遇到编译错误,请显示它们(剪切和粘贴,格式正确)。不要忘记显示导致错误生成的编译行。没有看到我们根本无能为力的实际问题。
-
我会指出您的
card.o规则出于某种原因使用NumberList.o的目标名称。我不知道这是否是问题所在,没有看到编译行和错误消息。 -
我确定不是后者,因为它会在所有对 tsllist.h 的引用和使用都被删除/注释掉时编译。
-
嗯,你应该从第一个错误开始(不是最后一个错误!越晚的错误越有可能是由早期的错误引起的)。它在第 5 行显示您的
card.h文件有问题。您没有向我们展示此文件或前 5 行包含的内容。我会说,我个人认为将using放入这样的头文件中是一个非常糟糕的主意。对于包含的文件,它可能会产生严重后果(using不会在文件末尾停止!!)。您应该只写出参考:它更清楚,而不是更多的努力。 -
我又把卡h和cpp文件放上去了。现在我要说 .h 文件中的几乎所有内容都是由讲师完成的,我真的不应该更改它(因此我专注于 tsllist.h 的问题,这是我的)。
标签: c++ c++11 templates makefile header