Reputation: 95
I'm trying to compile the following sources and the header:
5-book.cpp
#include "book.h"
int main()
try {
string isbn = "0-321-99278-4";
Book b {"harry potter","jk rowling",1998,isbn};
}
catch(runtime_error& e) {
cerr<<"error: "<<e.what()<<'\n';
}
book.h
#include <std_lib_facilities.h>
class Book {
static const int min_copyright = 1900;
public:
Book(string t,string a,int c,string s);
private:
string title;
string author;
int copyright;
string isbn;
};
bool is_isbn(string& isn);
int skip_int(string& s,int i);
bool alphameric(char c);
book.cpp
#include "book.h"
Book::Book(string t,string a,int c,string s) : title {t},author {a},copyright {c},isbn {s} {
if(c<min_copyright) error("not valid copyright year");
if(!is_isbn(s)) error("not valid ISBN");
}
bool is_isbn(string& isbn) {
int i = 0;
for(int j = 0; j<3; ++j) {
i = skip_int(isbn,i);
if(i==0 || i==isbn.size() || isbn[i++]!='-') return false;
}
if(i==isbn.size() || !alphameric(isbn[i++])) return false;
if(i!=isbn.size()) return false;
return true;
}
int skip_int(string& s,int i) {
int j = i;
for(; j<s.size() && ('0'<=s[j] && s[j]<='9'); ++j);
if(i==j) return 0;
return j;
}
bool alphameric(char c) {
return (('0'<=c && c<='9') || ('a'<=c && c<='z') || ('A'<=c && c<='Z'))? true : false;
}
But I get the following error.
Compilador de optimización de C/C++ de Microsoft (R) versión 19.26.28806 para x86
(C) Microsoft Corporation. Todos los derechos reservados.
5-book.cpp
book.cpp
Generating code...
Microsoft (R) Incremental Linker Version 14.26.28806.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:5-book.exe
5-book.obj
book.obj
book.obj : error LNK2005: already defined "class std::mersenne_twister_engine<unsigned int,32,624,397,31,2567483615,11,4294967295,7,2636928640,15,4022730752,18,1812433253> & __cdecl get_rand(void)" (?get_rand@@YAAAV?$mersenne_twister_engine@I$0CA@$0CHA@$0BIN@$0BP@$0JJAILANP@$0L@$0PPPPPPPP@$06$0JNCMFGIA@$0P@$0OPMGAAAA@$0BC@$0GMAHIJGF@@std@@XZ) en 5-book.obj
book.obj : error LNK2005: already defined "void __cdecl seed_randint(int)" (?seed_randint@@YAXH@Z) en 5-book.obj
5-book.exe : fatal error LNK1169: one or more symbols defined simultaneously
What I may be doing wrong with this code, or with the command?
Upvotes: 0
Views: 110
Reputation: 17454
This is a bug in your version of Stroustrup's std_lib_facilities.h
.
Neither get_rand
nor seed_randint
is marked inline
, and since they are defined in a header, their definitions will be embedded in every translation unit that makes use of the header, rather than just once (and not with internal linkage).
The version on GitHub does not have this problem, though ironically that seems to be because it is older.
Upvotes: 1