链接虚函数

时间:2016-05-04 23:11:48

标签: c++

我遇到了一个奇怪的C ++问题。好吧,我试着在七个独立的文件上写一个简单的程序。一切进展顺利,直到发生错误导致我无法继续前进。有人能说出我做错了什么吗?

的main.cpp

#include <iostream>
#include "item.hpp"
using namespace std;

int main()
{

int x;
Magazine some_magazine;
Book some_book;

Item *wsk_instr;

cout << "Witamy w ksiegarni" << endl << endl;
cout << "Wcisnij 1 aby kupic ksiazke" << endl;
cout << "Wcisnij 2 aby sprzedac ksiazke" << endl;
cout << "Wcisnij 3 aby zobaczyc jakie ksiazki mamy" << endl;
cin >> x;

switch (x)
{
    case 1:
        cout << "Kup ksiazke" <<endl;
        wsk_instr = &some_magazine;
        wsk_instr -> show();
        cout << "Ktora ksiazke chcesz kupic?" << endl;
        cout << "Ksiazka pierwsza - 1, ksiazka druga - 2, ksiazka trzecia - 3" << endl;
        wsk_instr = &some_book;
        wsk_instr -> sell();
        break;

    case 2:
        cout << "Sprzedaj ksiazke" << endl;
        cout << "Jaka ksiazke chcesz sprzedać? Podaj tytul:\t";
        wsk_instr = &some_book;
        wsk_instr -> buy();
        break;

    case 3:
        cout << "Pokaz co mamy na stanie" << endl;
        wsk_instr = &some_magazine;
        wsk_instr -> show();
        break;

    default:
        cout << "Nie wybrales nic" << endl;
        break;
}
}    

item.cpp&amp; item.hpp

item.cpp
#include <iostream>

void buy() {};
void sell() {};
void show() {};

item.hpp
#ifndef item_hpp
#define item_hpp

class Item {
public:
void virtual buy();
void virtual sell();
void virtual show();
};
#endif /* item_h */

magazine.cpp&amp; magazine.hpp

magazine.cpp
#include <iostream>
#include "magazine.hpp"
#include "book.hpp"

//dodawanie elementu na koniec wektora
void Magazine::add() {
std::cout << "Jaka ksiazke chcesz sprzedać? Podaj tytul:\t";
std::string book_title;
std::cin >> book_title;
table_of_books.push_back(book_title);
}

//usuwanie ostatniego elementu wektora
void Magazine::remove() {
std::cout << "Ktora ksiazke chcesz kupic? Podaj numer:\t";
int choose;
std::cin >> choose;
table_of_books.erase(table_of_books.begin() + choose);
}

//pokazywanie wielkosci wektora (ilosci elementow w nim)
void Magazine::show() {
std::cout << "W magazynie jest: " << table_of_books.size() << " ksiazek. A oto one:" << std::endl;
//wyswietlanie wszystkich ksiazek jakie sa w wektorze
for(int counter = 0; counter < table_of_books.size(); counter ++) {
    std::cout << table_of_books[counter] << std::endl;
    }    
}


magazine.hpp
#ifndef magazine_hpp
#define magazine_hpp
#include <vector>
#include "item.hpp"
#include "book.hpp"

class Magazine : public Item {
std::vector <std::string> table_of_books { "Potop", "Zemsta", "Dziady", };

public:
void add();
void remove();
void show();
};
#endif /* magazine_hpp */

book.cpp&amp; book.hpp

book.cpp
#include <iostream>
#include "book.hpp"
#include "magazine.hpp"

void Book::buy() {
add_book_to_the_magazine.add();
}

void Book::sell() {
delete_book_from_the_magazine.remove();
}

book.hpp
#ifndef book_hpp
#define book_hpp
#include "item.hpp"
#include "magazine.hpp"

class Book : public Item {

Magazine add_book_to_the_magazine;
Magazine delete_book_from_the_magazine;

void buy();
void sell();
};

#endif /* book_hpp */

我的问题在于这两行:

的main.cpp

Magazine some_magazine;
Book some_book;

Xcode说:未知的类型名称&#39;杂志&#39;和&#39; Book&#39;。听起来链接有问题吗?但是课堂杂志和课本被公开为班级项目。那么,问题是什么?

2 个答案:

答案 0 :(得分:1)

您需要在main.cpp的顶部包含book.hpp和magazine.hpp:

#include "magazine.hpp"
#include "book.hpp"

答案 1 :(得分:0)

您的main.cpp不包含magazine.hpp和book.hpp。