2

I am learning to code C++ in Xcode, i am doing this example from the book by Joe Pitt-Francis and Jonathan Whiteley. "Guide to Scientific Computing in C++"

The code is as follow,

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

When I complie, I got the follow error.

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)

In several forums it says that the issue is that Xcode is not compiling all the files, but I have no idea on how to solve it, any suggestion is more than welcome.

heri-salmas
  • 103
  • 2
  • 12

1 Answers1

3

You declare function Book::SetYearOfPublication() in Book.hpp, but you never define it. You have to do something like this in an implementation file:

void Book::SetYearOfPublication (int year) {
  // TODO: add code for setting the publication year
}
Oswald
  • 31,254
  • 3
  • 43
  • 68