gpt4 book ai didi

c++ - 体系结构 x86_64 : Xcode 5 Issues 的 undefined symbol

转载 作者:搜寻专家 更新时间:2023-10-31 01:45:36 26 4
gpt4 key购买 nike

我正在学习在 Xcode 中编写 C++ 代码,我正在做这个来自 Joe Pitt-Francis 和 Jonathan Whiteley 的书中的例子。 《C++科学计算指南》

代码如下,

书.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;

}

书.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 没有编译所有文件,但我不知道如何解决它,非常欢迎任何建议。

最佳答案

您在 Book.hpp 中声明函数 Book::SetYearOfPublication(),但您从未定义它。你必须在实现文件中做这样的事情:

void Book::SetYearOfPublication (int year) {
// TODO: add code for setting the publication year
}

关于c++ - 体系结构 x86_64 : Xcode 5 Issues 的 undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21966887/

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