Linker error (?)

This is a discussion on Linker error (?) within the C++ Programming forums, part of the General Programming Boards category; I'm thinking this is a problem with pre-compiled headers is VS... Errors: Code: 1>------ Build started: Project: 5.3, Configuration: Debug ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    Linker error (?)

    I'm thinking this is a problem with pre-compiled headers is VS...

    Errors:
    Code:
    1>------ Build started: Project: 5.3, Configuration: Debug Win32 ------
    1>Compiling...
    1>ISBN.cpp
    1>Book.cpp
    1>Generating Code...
    1>Skipping... (no relevant changes detected)
    1>5.3.cpp
    1>Linking...
    1>ISBN.obj : error LNK2005: "public: __thiscall ISBN::ISBN(char * const)" (??0ISBN@@QAE@QAD@Z) already defined in 5.3.obj
    1>ISBN.obj : error LNK2005: "public: __thiscall ISBN::ISBN(void)" (??0ISBN@@QAE@XZ) already defined in 5.3.obj
    1>ISBN.obj : error LNK2005: "public: bool __thiscall ISBN::compareTo(class ISBN)" (?compareTo@ISBN@@QAE_NV1@@Z) already defined in 5.3.obj
    1>ISBN.obj : error LNK2005: "public: __thiscall ISBN::~ISBN(void)" (??1ISBN@@QAE@XZ) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: __thiscall ISBN::ISBN(char * const)" (??0ISBN@@QAE@QAD@Z) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: __thiscall ISBN::ISBN(void)" (??0ISBN@@QAE@XZ) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: bool __thiscall ISBN::compareTo(class ISBN)" (?compareTo@ISBN@@QAE_NV1@@Z) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: __thiscall ISBN::~ISBN(void)" (??1ISBN@@QAE@XZ) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: __thiscall Book::Book(unsigned char,unsigned char,unsigned char,float,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,char * const)" (??0Book@@QAE@EEEMV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@00QAD@Z) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: __thiscall Book::~Book(void)" (??1Book@@QAE@XZ) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: void __thiscall Book::setDate(char,char,char)" (?setDate@Book@@QAEXDDD@Z) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: char * __thiscall Book::getDate(char *)" (?getDate@Book@@QAEPADPAD@Z) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: void __thiscall Book::setPrice(float)" (?setPrice@Book@@QAEXM@Z) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: float __thiscall Book::getPrice(void)" (?getPrice@Book@@QAEMXZ) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: void __thiscall Book::setAuthor(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setAuthor@Book@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Book::getAuthor(void)" (?getAuthor@Book@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: void __thiscall Book::setTitle(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setTitle@Book@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Book::getTitle(void)" (?getTitle@Book@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: void __thiscall Book::setPublisher(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setPublisher@Book@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Book::getPublisher(void)" (?getPublisher@Book@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: void __thiscall Book::setCity(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setCity@Book@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Book::getCity(void)" (?getCity@Book@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: class ISBN __thiscall Book::getISBN(void)" (?getISBN@Book@@QAE?AVISBN@@XZ) already defined in 5.3.obj
    1>Book.obj : error LNK2005: "public: bool __thiscall Book::compareTo(class Book)" (?compareTo@Book@@QAE_NV1@@Z) already defined in 5.3.obj
    1>C:\Users\Alex Smith\Documents\Visual Studio 2008\Projects\5.3\Debug\5.3.exe : fatal error LNK1169: one or more multiply defined symbols found
    1>Build log was saved at "file://c:\Users\Alex Smith\Documents\Visual Studio 2008\Projects\5.3\5.3\Debug\BuildLog.htm"
    1>5.3 - 25 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    book.cpp:
    Code:
    #include "StdAfx.h"
    #include "Book.h"
    
    using namespace std;
    
    Book::Book(unsigned char month, unsigned char day, unsigned char year, float price, string author, string title, string city, char isbn[11])
    {
    	this->month = month;
    	this->day = day;
    	this->year = year;
    	this->price = price;
    	this->author = author;
    	this->title = title;
    	this->city = city;
    	this->isbn = ISBN(isbn);
    }
    Book::~Book(void)
    {
    	free(date);
    	date = NULL;
    }
    void Book::setDate(char month, char day, char year)
    {
    	this->month = month;
    	this->day = day;
    	this->year = year;
    }
    char* Book::getDate(char *date)
    {
    	date = new char(10);
    	sprintf_s(date, sizeof(date), "&d-%d-%d", day, month, year);
    	return date;
    }
    void Book::setPrice(float price) { this->price = price; }
    float Book::getPrice() { return price; }
    void Book::setAuthor(string author) { this->author = author; }
    string Book::getAuthor() { return author; }
    void Book::setTitle(string title) { this->title = title; }
    string Book::getTitle() { return title; }
    void Book::setPublisher(string publisher) { this->publisher = publisher; }
    string Book::getPublisher() { return publisher; }
    void Book::setCity(string city) { this->city = city; }
    string Book::getCity() { return city; }
    ISBN Book::getISBN() { return isbn; }
    bool Book::compareTo(Book other)
    {
    	return (isbn.compareTo(other.getISBN()));
    }
    Book.h:
    Code:
    #pragma once
    #include <string>
    #include "ISBN.cpp"
    
    using namespace std;
    
    
    class Book
    {
    public:
    	Book(unsigned char month, unsigned char day, unsigned char year, float price, string author, string title, string city, char isbn[11]);
    	~Book(void);
    	void setDate(char month, char day, char year);
    	char* getDate(char *date);
    	void setPrice(float price);
    	float getPrice();
    	void setAuthor(string author);
    	string getAuthor();
    	void setTitle(string title);
    	string getTitle();
    	void setPublisher(string publisher);
    	string getPublisher();
    	void setCity(string city);
    	string getCity();
    	ISBN getISBN();
    	bool compareTo(Book other);
    private:
    	char month, day, year, *date;
    	float price;
    	string author;
    	string title;
    	string publisher;
    	string city;
    	ISBN isbn;
    };
    ISBN.cpp:
    Code:
    #include "StdAfx.h"
    #include "ISBN.h"
    #include <string>
    
    ISBN::ISBN(char str[11])
    {
    	char tmp[5];
    	int sum = 0;
    	for(int i = 0; i < 11; i++)
    	{
    		strncpy_s(tmp, str+i, 1);
    		sum = sum + atoi(tmp) * (i + 1);
    	}
    	if (sum / 11 == 10)
    		check = 'X';
    	else
    		check = sum / 11;
    	strncpy_s(tmp, str, 1);
    	group = atoi(tmp);
    	strncpy_s(tmp, str+2, 3);
    	publisher = atoi(tmp);
    	strncpy_s(tmp, str+6, 5);
    	id = atoi(tmp);
    }
    ISBN::ISBN(void)
    {
    }
    bool ISBN::compareTo(ISBN other)
    {
    	if(group == other.group && publisher == other.publisher && id == other.id)
    		return true;
    	else
    		return false;
    }
    ISBN::~ISBN(void)
    {
    }
    ISBN.h:

    Code:
    #pragma once
    
    
    class ISBN
    {
    public:
    	ISBN(char str[11]);
    	ISBN(void);
    	~ISBN(void);
    	char* getISBN(void);
    	bool checkISBN(void);
    	bool compareTo(ISBN other);
    private:
    	char check;
    	unsigned char group;
    	unsigned short publisher;
    	unsigned int id;
    };

    Any ideas?

    TIA

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You should be including headers (for declarations), not cpp files (implementations).

    BTW, ISBN should be 13 digits nowadays.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Yeah, you get that kind of thing happening when you have

    #include "ISBN.cpp"

    .cpp files go in the project, so they can be compiled ONCE only.

    If you start including source code, there is a very real chance you end up compiling the same code over and over, and leading to "multiply declared symbols".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    I thought that looked weird (it's the way VS setup)... and the ISBN is only 11 because the check (last number) and dash are added on later.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    How do you guys suggest I rearrange includes? I'm working on it right now but without much luck... TIA

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Right now you have book.cpp including book.h which includes isbn.cpp which includes isbn.h.

    1. All headers should have inclusion guards present (don't know how portable #pragma once is).
    2. Book.H should not include ISBN.cpp but rather ISBN.H
    3. It looks like your 5.3.cpp file is including both ISBN.cpp and book.cpp (at the very least it is probably including book.cpp) but I can't confirm this, just guessing based on the output from the build. This needs to include book.h instead at the least but can also include isbn.h.
    4. Any cpp file should be listed in your project's source files.
    5. Remove the using namespace std; bit in book.h and explicitly qualify any necessary items, i.e. string objects.
    Last edited by hk_mp5kpdw; 03-18-2010 at 12:08 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    Thanks for the advice, added inclusion gaurds
    now it looks something like this:

    main --> Book.h --> Book.cpp

    but now all the elements in the .cpp are not declared and/or are invalid... Did I miss something here?

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Post the same build log/files that you did in your original post so we can see what you've done.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Include guards don't help.

    If you're compiling foo.cpp, all include guards will do is ensure foo.cpp doesn't get more than one copy of book.cpp.

    If you then compile bar.cpp, which also includes book.h (and the .cpp), you've got ANOTHER copy of the functions, and you're right back at the "multiply declared" errors.

    DO NOT #include source code!
    Add your .cpp files to the project, then fix it so each .cpp file includes the header files it needs.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    (switched to mac in the middle)

    main.cpp:
    Code:
    #include <iostream>
    #include "Book.h"
    #include "ISBN.h"
    
    
    
    
    int main (int argc, char * const argv[]) {
        // insert code here...
    	Book myBook = Book();
        std::cout << "Hello, World!\n";
        return 0;
    }
    Book.h:
    Code:
    #include <iostream>
    #include "Book.h"
    #include "ISBN.h"
    
    
    
    
    int main (int argc, char * const argv[]) {
        // insert code here...
    	Book myBook = Book();
        std::cout << "Hello, World!\n";
        return 0;
    }
    ISBN.h
    Code:
    #include "ISBN.cpp"
    
    
    #ifndef _ISBN_H_
    #define _ISBN_H_
    class ISBN
    {
    	public:
    	ISBN(char str[11]);
    	ISBN(void);
    	~ISBN(void);
    	char* getISBN(void);
    	bool checkISBN(void);
    	bool compareTo(ISBN other);
    	private:
    		char check;
    		unsigned char group;
    		unsigned short publisher;
    		unsigned int id;
    };
    #endif
    Book.cpp:

    Code:
    #include "ISBN.cpp"
    
    
    #ifndef _ISBN_H_
    #define _ISBN_H_
    class ISBN
    {
    	public:
    	ISBN(char str[11]);
    	ISBN(void);
    	~ISBN(void);
    	char* getISBN(void);
    	bool checkISBN(void);
    	bool compareTo(ISBN other);
    	private:
    		char check;
    		unsigned char group;
    		unsigned short publisher;
    		unsigned int id;
    };
    #endif
    ISBN.cpp:
    Code:
    #include <string>
    
    
    ISBN::ISBN(char str[11])
    {
    	char tmp[5];
    	int sum = 0;
    	for(int i = 0; i < 11; i++)
    	{
    		strncpy(tmp, str+i, 1);
    		sum = sum + atoi(tmp) * (i + 1);
    	}
    	if (sum / 11 == 10)
    		check = 'X';
    	else
    		check = sum / 11;
    	strncpy(tmp, str, 1);
    	group = atoi(tmp);
    	strncpy(tmp, str+2, 3);
    	publisher = atoi(tmp);
    	strncpy(tmp, str+6, 5);
    	id = atoi(tmp);
    }
    ISBN::ISBN(void)
    {
    }
    bool ISBN::compareTo(ISBN other)
    {
    	if(group == other.group && publisher == other.publisher && id == other.id)
    		return true;
    	else
    		return false;
    }
    ISBN::~ISBN(void)
    {
    }
    Errors:
    Code:
    Line Location main.cpp:10: warning: statement is a reference, not call, to function 'Book'
    Line Location main.cpp:10: error: expected `;' before 'myBook'
    Line Location ISBN.cpp:34: error: expected constructor, destructor, or type conversion before '::' token
    Line Location ISBN.cpp:34: error: expected constructor, destructor, or type conversion before '::' token
    Line Location ISBN.cpp:34: error: expected constructor, destructor, or type conversion before '::' token
    Line Location ISBN.cpp:28: error: expected ',' or ';' before '{' token
    Line Location ISBN.cpp:28: error: expected ',' or ';' before '{' token
    Line Location ISBN.cpp:28: error: expected ',' or ';' before '{' token
    Line Location ISBN.cpp:27: error: cannot resolve overloaded function 'ISBN' based on conversion to type 'bool'
    Line Location ISBN.cpp:27: error: cannot resolve overloaded function 'ISBN' based on conversion to type 'bool'
    Line Location ISBN.cpp:27: error: expected `)' before 'other'
    Line Location ISBN.cpp:27: error: 'ISBN' is not a class or namespace
    Line Location ISBN.cpp:27: error: expected `)' before 'other'
    Line Location ISBN.cpp:27: error: 'ISBN' is not a class or namespace
    Line Location ISBN.cpp:27: error: 'ISBN' is not a class or namespace
    Line Location ISBN.cpp:27: error: cannot resolve overloaded function 'ISBN' based on conversion to type 'bool'
    Line Location ISBN.cpp:27:        'bool compareTo' previously defined here
    Line Location ISBN.cpp:27:        'bool compareTo' previously defined here
    Line Location ISBN.cpp:27: error: expected `)' before 'other'
    Line Location ISBN.cpp:26: warning: control reaches end of non-void function
    Line Location ISBN.cpp:26: warning: control reaches end of non-void function
    Line Location ISBN.cpp:26: warning: control reaches end of non-void function
    Line Location ISBN.cpp:24: error: 'ISBN' is not a class or namespace
    Line Location ISBN.cpp:24: error: ISO C++ forbids declaration of 'ISBN' with no type
    Line Location ISBN.cpp:24: error: ISO C++ forbids declaration of 'ISBN' with no type
    Line Location ISBN.cpp:24: error: 'ISBN' is not a class or namespace
    Line Location ISBN.cpp:24: error: ISO C++ forbids declaration of 'ISBN' with no type
    Line Location ISBN.cpp:24: error: 'ISBN' is not a class or namespace
    Line Location ISBN.cpp:23: warning: control reaches end of non-void function
    Line Location ISBN.cpp:23: warning: control reaches end of non-void function
    Line Location ISBN.cpp:23: warning: control reaches end of non-void function
    Line Location ISBN.cpp:22: error: 'id' was not declared in this scope
    Line Location ISBN.cpp:22: error: 'id' was not declared in this scope
    Line Location ISBN.cpp:22: error: 'id' was not declared in this scope
    Line Location ISBN.cpp:20: error: 'publisher' was not declared in this scope
    Line Location ISBN.cpp:20: error: 'publisher' was not declared in this scope
    Line Location ISBN.cpp:20: error: 'publisher' was not declared in this scope
    Line Location ISBN.cpp:18: error: 'group' was not declared in this scope
    Line Location ISBN.cpp:18: error: 'group' was not declared in this scope
    Line Location ISBN.cpp:18: error: 'group' was not declared in this scope
    Line Location ISBN.cpp:16: error: 'check' was not declared in this scope
    Line Location ISBN.cpp:16: error: 'check' was not declared in this scope
    Line Location ISBN.cpp:16: error: 'check' was not declared in this scope
    Line Location ISBN.cpp:14: error: 'check' was not declared in this scope
    Line Location ISBN.cpp:14: error: 'check' was not declared in this scope
    Line Location ISBN.cpp:14: error: 'check' was not declared in this scope
    Line Location ISBN.cpp:4: error: 'ISBN' has not been declared
    Line Location ISBN.cpp:4: error: 'ISBN' has not been declared
    Line Location ISBN.cpp:4: error: 'ISBN' has not been declared
    Line Location ISBN.cpp:4: error: ISO C++ forbids declaration of 'ISBN' with no type
    Line Location ISBN.cpp:4: error: ISO C++ forbids declaration of 'ISBN' with no type
    Line Location ISBN.cpp:4: error: ISO C++ forbids declaration of 'ISBN' with no type
    Line Location Book.h:37: error: 'ISBN' does not name a type
    Line Location Book.h:28: error: 'ISBN' does not name a type
    Line Location Book.cpp:42: error: redefinition of 'bool compareTo'
    Line Location Book.cpp:42: error: expected `)' before 'other'
    Line Location Book.cpp:42: error: 'Book' is not a class or namespace
    Line Location Book.cpp:42: error: 'Book' is not a class or namespace
    Line Location Book.cpp:42: error: expected `)' before 'other'
    Line Location Book.cpp:42: error: redefinition of 'bool compareTo'
    Line Location Book.cpp:41: error: 'ISBN' does not name a type
    Line Location Book.cpp:41: error: 'ISBN' does not name a type
    Line Location Book.cpp:40: error: 'string' does not name a type
    Line Location Book.cpp:40: error: 'string' does not name a type
    Line Location Book.cpp:39: error: expected ',' or ';' before '{' token
    Line Location Book.cpp:39: error: variable or field 'setCity' declared void
    Line Location Book.cpp:39: error: 'string' was not declared in this scope
    Line Location Book.cpp:39: error: 'Book' is not a class or namespace
    Line Location Book.cpp:39: error: expected ',' or ';' before '{' token
    Line Location Book.cpp:39: error: 'Book' is not a class or namespace
    Line Location Book.cpp:39: error: 'string' was not declared in this scope
    Line Location Book.cpp:39: error: variable or field 'setCity' declared void
    Line Location Book.cpp:38: error: 'string' does not name a type
    Line Location Book.cpp:38: error: 'string' does not name a type
    Line Location Book.cpp:37: error: variable or field 'setPublisher' declared void
    Line Location Book.cpp:37: error: 'Book' is not a class or namespace
    Line Location Book.cpp:37: error: 'string' was not declared in this scope
    Line Location Book.cpp:37: error: variable or field 'setPublisher' declared void
    Line Location Book.cpp:37: error: 'Book' is not a class or namespace
    Line Location Book.cpp:37: error: expected ',' or ';' before '{' token
    Line Location Book.cpp:37: error: 'string' was not declared in this scope
    Line Location Book.cpp:37: error: expected ',' or ';' before '{' token
    Line Location Book.cpp:36: error: 'string' does not name a type
    Line Location Book.cpp:36: error: 'string' does not name a type
    Line Location Book.cpp:35: error: 'Book' is not a class or namespace
    Line Location Book.cpp:35: error: variable or field 'setTitle' declared void
    Line Location Book.cpp:35: error: 'string' was not declared in this scope
    Line Location Book.cpp:35: error: expected ',' or ';' before '{' token
    Line Location Book.cpp:35: error: expected ',' or ';' before '{' token
    Line Location Book.cpp:35: error: 'string' was not declared in this scope
    Line Location Book.cpp:35: error: variable or field 'setTitle' declared void
    Line Location Book.cpp:35: error: 'Book' is not a class or namespace
    Line Location Book.cpp:34: error: 'string' does not name a type
    Line Location Book.cpp:34: error: 'string' does not name a type
    Line Location Book.cpp:33: error: 'string' was not declared in this scope
    Line Location Book.cpp:33: error: 'Book' is not a class or namespace
    Line Location Book.cpp:33: error: variable or field 'setAuthor' declared void
    Line Location Book.cpp:33: error: 'string' was not declared in this scope
    Line Location Book.cpp:33: error: expected ',' or ';' before '{' token
    Line Location Book.cpp:33: error: variable or field 'setAuthor' declared void
    Line Location Book.cpp:33: error: 'Book' is not a class or namespace
    Line Location Book.cpp:33: error: expected ',' or ';' before '{' token
    Line Location Book.cpp:32: error: 'price' was not declared in this scope
    Line Location Book.cpp:32: error: 'Book' is not a class or namespace
    Line Location Book.cpp:32: error: 'price' was not declared in this scope
    Line Location Book.cpp:32: error: 'Book' is not a class or namespace
    Line Location Book.cpp:31: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:31: error: 'Book' is not a class or namespace
    Line Location Book.cpp:31: error: 'Book' is not a class or namespace
    Line Location Book.cpp:31: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:28: error: 'year' was not declared in this scope
    Line Location Book.cpp:28: error: 'month' was not declared in this scope
    Line Location Book.cpp:28: error: 'month' was not declared in this scope
    Line Location Book.cpp:28: error: 'day' was not declared in this scope
    Line Location Book.cpp:28: error: 'day' was not declared in this scope
    Line Location Book.cpp:28: error: 'year' was not declared in this scope
    Line Location Book.cpp:25: error: 'Book' is not a class or namespace
    Line Location Book.cpp:25: error: 'Book' is not a class or namespace
    Line Location Book.cpp:23: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:23: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:22: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:22: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:21: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:21: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:19: error: 'Book' is not a class or namespace
    Line Location Book.cpp:19: error: 'Book' is not a class or namespace
    Line Location Book.cpp:14: error: expected constructor, destructor, or type conversion before '::' token
    Line Location Book.cpp:14: error: expected constructor, destructor, or type conversion before '::' token
    Line Location Book.cpp:13: warning: control reaches end of non-void function
    Line Location Book.cpp:13: warning: control reaches end of non-void function
    Line Location Book.cpp:12: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:12: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:11: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:11: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:10: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:10: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:9: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:9: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:8: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:8: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:7: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:7: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:6: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:6: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:5: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:5: error: invalid use of 'this' in non-member function
    Line Location Book.cpp:3: error: 'Book' has not been declared
    Line Location Book.cpp:3: error: 'string' has not been declared
    Line Location Book.cpp:3: error: 'string' has not been declared
    Line Location Book.cpp:3: error: 'string' has not been declared
    Line Location Book.cpp:3: error: ISO C++ forbids declaration of 'Book' with no type
    Line Location Book.cpp:3: error: ISO C++ forbids declaration of 'Book' with no type
    Line Location Book.cpp:3: error: 'string' has not been declared
    Line Location Book.cpp:3: error: 'string' has not been declared
    Line Location Book.cpp:3: error: 'string' has not been declared
    Line Location Book.cpp:3: error: 'Book' has not been declared

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    OK, a new more basics.

    1. ONLY ONE file has a main().

    2. book.cpp includes book.h, ISBN.cpp includes ISBN.h etc etc

    3. If book.cpp NEEDS something from ISBN, then it includes ISBN.h

    4. main.cpp, book.cpp, ISBN.cpp (and any other .cpp) go into the project settings->source files.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    You're still including *.cpp files, and you shouldn't be.
    book.h is including itself, and it shouldn't be.
    There is an include outside of ISBN.h's include gaurds, and there shouldn't be.
    ISBN.h is including ISBN.cpp, and it shouldn't be.
    Same issue with book.cpp
    Book.cpp isn't including Book.h, and it should be.
    ISBN.cpp isn't including ISBN.h, and it should be.

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    I know there's only one main(), I switched between Mac and Windows in the middle (slightly different file names)

    Includes should look like this:
    main.cpp(whatever file contains main()):
    -book.cpp

    book.cpp:
    -book.h
    -ISBN.h

    ISBN.cpp:
    -ISBN.h

    ISBN.h:
    -none

    book.h:
    (needs something from ISBN, but including either ISBN file causes problems, not having one in there doesn't work)

    and to top it off I think I may have copied and pasted some of those files incorrectly
    (back in windows)

    (pasted correctly this time)

    main:
    Code:
    #pragma once
    #include "Book.cpp"
    #include "StdAfx.h"
    Book.cpp:
    Code:
    #pragma once
    #include "StdAfx.h"
    #include "Book.h"
    #include "ISBN.h"
    book.h:
    Code:
    #pragma once
    #include <string>
    #include "ISBN.h"
    
    using namespace std;
    ISBN.cpp:
    Code:
    #include "StdAfx.h"
    #pragma once
    #include "ISBN.h"
    #include <string>



    I apologize if I'm a little thick but I didn't sleep last night and it's really catching up with me.

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    You're still including Book.cpp. Don't. Include book.h is you need to.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  15. #15
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    You keep asking for help, and keep ignoring the advice you are given
    Some additional things besides what has already been said before:
    Code:
    #pragma once
    No need to use this in .cpp files. The IDE will only compile each .cpp file once anyway. (unless you are including them in other .cpp files, like you've been told you shouldn't do)

    Code:
    #include "StdAfx.h"
    If you are using precompiled headers then this has to be the first #include in every .cpp file or you will get a compilation error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21