一段时间以来,我一直在Xcode中进行此C ++编程任务,并且在我的代码中遇到持续的clang错误:
Undefined symbols for architecture x86_64:
"Card::print()", referenced from:
_main in main.o
"Card::getBookID()", referenced from:
checkOutBook(Book*, Card*, int, int) in main.o
"Card::getCardID()", referenced from:
checkOutBook(Book*, Card*, int, int) in main.o
checkInBook(Book*, Card*, int, int) in main.o
"Card::setBookID(int)", referenced from:
checkOutBook(Book*, Card*, int, int) in main.o
checkInBook(Book*, Card*, int, int) in main.o
"Card::Card(int, char*, char*)", referenced from:
createCard(int, char*, char*) in main.o
"Card::Card()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
经过数小时令人沮丧的论坛搜索后,我希望有人能为我提供帮助。到目前为止,我的代码是:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string.h>
using namespace std;
class Card
{
private:
char cardName[30];
char phoneNumber[15];
int CardID;
int BookID;
public:
Card();
Card(int c, char n[], char p[]);
void setBookID(int b);
int getBookID();
int getCardID();
void print();
};
class Book
{
private:
int BookID;
int CardID;
char title[30];
char author[10];
public:
Book();
Book(int b, char t[], char a[]);
void setCardID(int c);
int getCardID();
int getBookID();
void print();
};
Book::Book()
{
BookID = 0;
CardID = 0;
title[0] = '\0';
author[0] = '\0';
}
Book::Book(int b, char t[], char a[])
{
BookID = b;
strcpy(title, t);
strcpy(author, a);
CardID = 0;
}
void Book::setCardID(int c)
{
CardID = c;
}
int Book::getCardID()
{
return CardID;
}
int Book::getBookID()
{
return BookID;
}
void Book::print()
{
cout << title << endl
<< author << endl
<< "Book ID: " << BookID << endl;
if(CardID == 0)
{
cout << "Not Checked Out." << endl << endl;
}
else
{
cout << "Checked Out to: " << CardID << endl << endl;
}
}
//Begin
void ShowMenu();
void checkOutBook(Book books[], Card cards[], int numBooks, int numCards);
void checkInBook(Book books[], Card cards[], int numBooks, int numCards);
Card createCard(int cardID, char name[], char phone[]);
int main()
{
int command = 0;
// declare an array of 20 cards
Card cards[20];
int numCards = 0;
// declare an array of 20 books
Book books[20];
int numBooks = 0;
// declare a file pointer and open the cards file
ifstream cardInput("cards.txt");
if(!cardInput)
{
cout << "ERRORL Cannot open file 'cards.txt'" << endl;
exit(1);
}
cout << "Reading cards.txt file. . ." << endl;
char name[25];
char phone[15];
int cardID = 0;
char junk[25];
// read the next line from the file
cardInput.getline(name, 25, ','); // Read first person's name
while (cardInput)
{
cardInput.getline(junk, 5, ' '); // Read past blank space
cardInput.getline(phone, 15, ',');
cardInput.getline(junk, 5, ' ');
cardInput >> cardID;
cardInput.getline(junk, 5, '\n');
// Create a card
cards[numCards] = createCard(cardID, name, phone);
numCards++;
cardInput.getline(name, 25, ',');
}
// create a card and store it in the array of cards
// declare a file pointer and open the books file
ifstream bookInput("books.txt");
if(!cardInput)
{
cout << "ERROR: Cannot open file 'books.txt'" << endl;
exit(1);
}
cout << "Reading books.txt file. . ." << endl;
char title[25];
char author[15];
int bookID = 0;
char junk2[25];
// read the next line from the file
bookInput.getline(title, 25, ','); // Read title of first book
while (bookInput)
{
bookInput.getline(junk2, 5, ' '); // Read past blank space
bookInput.getline(author, 15, ',');
bookInput.getline(junk2, 5, ' ');
bookInput >> bookID;
bookInput.getline(junk2, 5, '\n');
// create a books and store it in the array of books
// display main menu
ShowMenu();
cin >> command;
while(command != 0)
{
switch(command){
case 1: // Show all Library Cards
for(int i = 0; i < numCards; i++)
{
cards[i].print();
}
break;
case 2: // Show all Books
for(int i=0; i < numBooks; i++)
{
books[i].print();
}
break;
case 3: // Check out a Book
checkOutBook(books, cards, numBooks, numCards);
break;
case 4: // Check in a Book
checkInBook(books, cards, numBooks, numCards);
break;
case 5: // Create a new Library Card
cout << "Enter your name: ";
cin.getline(junk, 2, '\n');
cin.getline(name, 25, '\n');
cout << "Enter your phone number: ";
cin.getline(phone, 15, '\n');
cout << "Enter the card ID: ";
cin >> cardID;
// Create card and store it in the next array location
cards[numCards] = createCard(cardID, name, phone);
numCards++;
break;
default:
cout << "ERROR: Values must be between 1 and 5." << endl;
}
ShowMenu();
cin >> command;
}
}
return 0;
}
void ShowMenu()
{
cout << "=====[ M A I N M E N U ]=====" << endl;
cout << " 1. Show all Library Cards " << endl;
cout << " 2. Show all Books " << endl;
cout << " 3. Check out a Book " << endl;
cout << " 4. Check in a Book " << endl;
cout << " 5. Create a New Library Card" << endl;
cout << " 6. Exit the System " << endl;
}
void checkOutBook(Book books[], Card cards[], int numBooks, int numCards)
{
int card = 0, book = 0;
int cardid = 0, bookid = 0;
// Enter Card ID
cout << "Enter your Card ID: ";
cin >> cardid;
// Find Card ID
while(card < numCards && cardid != cards[card].getCardID())
{
card++;
}
// If not found, return
if(card == numCards)
{
cout << "ERROR: Invalid Card ID. Create a new card before retrying." << endl;
return;
}
// If found, does the person have a book checked out?
// If yes, return
if (cards[card].getBookID() != 0)
{
cout << "ERROR: You already have a book checked out. Please return it first before retrying." << endl;
return;
}
// Card validated!
// Enter Book ID
cout << "Enter the Book ID: ";
cin >> bookid;
// Find Book ID
while(book < numBooks && bookid != books[book].getBookID())
{
book++;
}
// If not found, return
if(book == numBooks)
{
cout << "ERROR: Invalid Book ID. Try another book." << endl;
return;
}
// If found, is it checked out?
// If yes, return
if(books[book].getCardID() != 0)
{
cout << "ERROR: This book is already checked out. Come back later." << endl;
return;
}
// Book validated!
// Check out the book
cards[card].setBookID(bookid);
books[book].setCardID(cardid);
}
void checkInBook(Book books[], Card cards[], int numBooks, int numCards)
{
int card = 0, book = 0;
int cardid = 0, bookid = 0;
// Enter Card ID
cout << "Enter your Card ID: ";
cin >> cardid;
// Find Card ID
while(card < numCards && cardid != cards[card].getCardID())
{
card++;
}
// If not found, return
if(card == numCards)
{
cout << "ERROR: Invalid Card ID. Create a new card before retrying." << endl;
return;
}
// Card found!
// Enter Book ID
cout << "Enter the Book ID: ";
cin >> bookid;
// Find Book ID
while(book < numBooks && bookid != books[book].getBookID())
{
book++;
}
// If not found, return
if(book == numBooks)
{
cout << "ERROR: Invalid Book ID. Try another book." << endl;
return;
}
// Check out the book
cards[card].setBookID(0);
books[book].setCardID(0);
}
Card createCard(int cardID, char name[], char phone[])
{
Card temp(cardID, name, phone);
return temp;
}
我意识到这太长了,但是我正在尽力而为。我对C ++和Xcode还是很陌生,不是很擅长导航菜单来尝试人们在其他帖子上建议的所有修复程序。我对clang错误的了解程度是,它可能意味着某个地方重复的功能或某些设置错误。我真的很感谢任何建议,在此先感谢您。