我正在学习在Xcode中编写C ++代码,我在Joe Pitt-Francis和Jonathan Whiteley的书中做了这个例子。 “C ++中的科学计算指南”
代码如下,
Book.ccp
#include "Book.hpp"
#include <iostream>
int main (int argc, char* argv[])
{
Book good_read;
good_read.author = "Lewis Carroll";
good_read.title = "Alice ";
good_read.publisher = "MA";
good_read.price = 199;
good_read.format = "hardback";
good_read.SetYearOfPublication(1787);
std::cout << "Year of publication of " << good_read.title << " is " << good_read.GetYearOfPublication() << "\n";
Book another_book(good_read);
Book an_extra_book ("the magic");
return 0;
}
Book.hpp
#ifndef book_Header_h
#define book_Header_h
#include <string>
class Book
{
public:
Book ();
Book (const Book& otherBook);
Book (std::string bookTitle);
std::string author, title, publisher, format;
int price;
void SetYearOfPublication (int year);
int GetYearOfPublication() const;
private:
int mYearOfPublication;
};
#endif
当我编译时,我得到了跟随错误。
Undefined symbols for architecture x86_64:
"Book::SetYearOfPublication(int)", referenced from:
_main in Book.o
"Book::Book(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in Book.o
"Book::Book(Book const&)", referenced from:
_main in Book.o
"Book::Book()", referenced from:
_main in Book.o
"Book::GetYearOfPublication() const", referenced from:
_main in Book.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
在几个论坛中它说问题是Xcode没有编译所有文件,但我不知道如何解决它,任何建议都非常受欢迎。
答案 0 :(得分:3)
您在 Book.hpp 中声明了函数Book::SetYearOfPublication()
,但您从未定义它。您必须在实现文件中执行以下操作:
void Book::SetYearOfPublication (int year) {
// TODO: add code for setting the publication year
}