• Advertisement

paintstripper88

Member
  • Content count

    13
  • Joined

  • Last visited

Community Reputation

100 Neutral

About paintstripper88

  • Rank
    Member
  1. Vectors of Classes

    I changed the line to ref.push_back(Reference("Book")); Now I am getting the error: New Reference Class has exited due to signal 11 (SIGSEGV). Can anyone help me, I don't know what to put in the copy constructor or the operator overloading. Thanks Dan
  2. Vectors of Classes

    Why do I need to copy the ref class variables? There is only one class, the Reference class, and what does the operator function do? I don't really understand what I have to put in those two functions that you suggested to add. Thanks Dan
  3. Vectors of Classes

    I want to create a vector of the class that I am working with. I can currently successfully create an array with the class, but I am having troubles with the vector class. Here is the code that I have so far: #include <string> #include <vector> #include <iostream> using namespace std; ////////////////////////////////////////////////////////////////// // R E F E R E N C E C L A S S ****// ////////////////////////////////////////////////////////////////// class Reference // Parent { private: //declare all variables string Author, Title, Journal, Year, Volume, Number, Pages, Note; string Key, Booktitle, Editor, Organisation, Publisher, Address, Month; string Series, Edition, Institution, Type, Howpublished; string ReferenceType; public: Reference(string refType) { ReferenceType = refType; } //set functions void setAuthor(string author); void setTitle(string title); void setJournal(string journal); void setYear(string year); void setVolume(string volume); void setNumber(string number); void setPages(string pages); void setNote(string note); void setKey(string key); void setBooktitle(string booktitle); void setEditor(string editor); void setOrganisation(string organisation); void setPublisher(string publisher); void setAddress(string address); void setMonth(string month); void setSeries(string series); void setEdition(string edition); void setInstitution(string institution); void setType(string type); void setHowpublished(string howpublished); //get functions string getAuthor(); string getTitle(); string getJournal(); string getYear(); string getVolume(); string getNumber(); string getPages(); string getNote(); string getKey(); string getBooktitle(); string getEditor(); string getOrganisation(); string getPublisher(); string getAddress(); string getMonth(); string getSeries(); string getEdition(); string getInstitution(); string getType(); string getHowpublished(); string ReturnReference(); }; //set functions void Reference::setAuthor(string author) {Author = author;} void Reference::setTitle(string title) {Title = title;} void Reference::setJournal(string journal) {Journal = journal;} void Reference::setYear(string year) {Year = year;} void Reference::setVolume(string volume) {Volume = volume;} void Reference::setNumber(string number) {Number = number;} void Reference::setPages(string pages) {Pages = pages;} void Reference::setNote(string note) {Note = note;} void Reference::setKey(string key) {Key = key;} void Reference::setBooktitle(string booktitle) {Booktitle = booktitle;} void Reference::setEditor(string editor) {Editor = editor;} void Reference::setOrganisation(string organisation) {Organisation = organisation;} void Reference::setPublisher(string publisher) {Publisher = publisher;} void Reference::setAddress(string address) {Address = address;} void Reference::setMonth(string month) {Month = month;} void Reference::setSeries(string series) {Series = series;} void Reference::setEdition(string edition) {Edition = edition;} void Reference::setInstitution(string institution) {Institution = institution;} void Reference::setType(string type) {Type = type;} void Reference::setHowpublished(string howpublished) {Howpublished = howpublished;} //get funcitons string Reference::getAuthor() {return Author;} string Reference::getTitle() {return Title;} string Reference::getJournal() {return Journal;} string Reference::getYear() {return Year;} string Reference::getVolume() {return Volume;} string Reference::getNumber() {return Number;} string Reference::getPages() {return Pages;} string Reference::getNote() {return Note;} string Reference::getKey() {return Key;} string Reference::getBooktitle() {return Booktitle;} string Reference::getEditor() {return Editor;} string Reference::getOrganisation() {return Organisation;} string Reference::getPublisher() {return Publisher;} string Reference::getAddress() {return Address;} string Reference::getMonth() {return Month;} string Reference::getSeries() {return Series;} string Reference::getEdition() {return Edition;} string Reference::getInstitution() {return Institution;} string Reference::getType() {return Type;} string Reference::getHowpublished() {return Howpublished;} string Reference::ReturnReference() { if (ReferenceType == "Book") { //output book reference string tempRef; string tempYear; string tempEd; string tempVol; string tempEdVol=""; string tempPub; if (Year != "") //if the year exists, include it { tempYear = "(" + Year + ")."; } if (Edition != "") //check if edition exists { tempEd = Edition + " ed."; } if (Volume != "") //check if volume exists { tempVol = "Vol " + Volume + "."; } if ((Volume != "") && (Edition != "")) //if they both exist { tempEdVol = "(" + tempEd + " " + tempVol + ")."; } if ((Volume == "") && (Edition != "")) //just edition exists { tempEdVol = "(" + tempEd + ")."; } if ((Volume != "") && (Edition == "")) //just volume exists { tempEdVol = "(" + tempVol + ")."; } if (Publisher != "") {tempPub = ":" + Publisher;} tempRef = Author + " " + tempYear + " " + Title + " " + tempEdVol + " " + Address + tempPub; return tempRef; } } int main() { //Reference aBook("Book"); //aBook.setAuthor("Daniel Beard"); //aBook.setTitle("This is the title"); //aBook.setYear("1999"); //aBook.setEdition("Fifth"); //aBook.setVolume("3"); //aBook.setPublisher("Cheese Publishing"); //aBook.setAddress("Perth"); vector<Reference> ref; int i; i = 1; ref.push_back("Book"); ref[i].setAuthor("Daniel"); ref[i].setTitle("This is a title"); ref[i].setYear("1999"); //cout << aBook.ReturnReference() << endl; cout << ref[i].ReturnReference() << endl; } The commented out code is the code that works, however, I would prefer to be using vectors rather than arrays, so I tried to create new vectors of the class. The above code gives a compile error : /Users/dan/Desktop/New Reference Class/main.cp:194: error: no matching function for call to 'std::vector<Reference, std::allocator<Reference> >::push_back(const char [5])' How would I call a new vector of the Reference class? Thanks, Dan
  4. Inheritance Question

    This should help explain things. A reference can have any of the following fields: Author Title Journal Year Volume Number Pages Month Note Key Booktitle Editor Organisation Publisher Address Month Series Edition Institution Type Howpublsihed And a book should only have these fields: Author, Title, Publisher, Year, Volume, Series, Address, Edition, Month, Note and Key. Is there a way to define all of the get and set functions, then in derived classes, define which ones can be used? Dan
  5. Inheritance Question

    Not sure if this is just an easy solution, but I am new to inheritance programming. I have some code that is as follows: #include <string> #include <vector> #include <iostream> using namespace std; class Reference // Parent { private: public: string getTitle(); }; class Book : public Reference // Derived { private: string Author, Title, Publisher, Year, Volume, Series, Address; string Edition, Month, Note, Key; public: void setAuthor(string author); void setTitle(string title); void setPublisher(string publisher); void setYear(string year); string getAuthor(); }; string Reference::getTitle() { return(Title); } void Book::setAuthor(string author) { Author = author; } void Book::setTitle(string title) {Title = title;} //set the book title void Book::setPublisher(string publisher) {Publisher = publisher;} //set the book's publisher void Book::setYear(string year) {Year = year;} //set the book year string Book::getAuthor() {return Author;} //function to return the author int main() { Book aBook; string tempAuthor; string tempTitle; aBook.setAuthor("This is an author"); //aBook.setTitle("This is a title"); tempAuthor = aBook.getAuthor(); //tempTitle = aBook.getTitle(); cout << tempAuthor << endl; cout << tempTitle << endl; } This code works as intended, but my question is, I am using the derived class (Book) which is a type of Reference. A book has some of the same properties as other References. Is there a way to define all of the functions in the Reference class, such as setTitle, setAuthor, setPublisher, etc ... and then allow the Book class to only be able to access a few of these functions? Is there a way to define in the Book class, which functions in the Reference class that it is allowed to access? I hope I have explained it clear enough... Thanks, Dan
  6. Really confusing problem..

  7. Class Instances

    The NewReference class is named 'NewReference', because I am creating a program that formats referencing files for a certain end text referencing format, in this case APA 5th. Thanks for the advice, using the constructor instead of set functions will help a lot.
  8. Class Instances

    That is exactly what I needed, thanks.
  9. Class Instances

    I am having trouble creating new instances of classes. This is the code for my class: #include <string> #include <iostream> #include <fstream> using namespace std; class NewReference { public: NewReference() { } virtual ~NewReference() { } virtual void CreateNewReference() const = 0; //virtual void DrawFill() const = 0; }; class Book : public NewReference { public: void set_author(string theauthor); string Book::get_author(); Book() { } virtual ~Book() { } virtual void CreateNewReference() const { cout << "TESTTESTEST" << endl; } private: string Author; }; void Book::set_author(string theauthor) { Author = theauthor; } string Book::get_author() { return Author; } And this is the code that I am using to create a new instance of the Book class. Book bk; bk.CreateNewReference(); The above code works fine, but how would I create an array of the book class, so that I can use bk like this: bk[1].CreateNewReference(); Or with a loop: bk[i].CreateNewReference(); Any help is appreciated.
  10. stl map help

    Firstly, include the classes at the top, if you haven't already done so, #include <string> #include <map> Then use this line after the includes, using namespace std; This eliminates the need for std::string, you can just use string instead. This may not fix the problem, but it will help to clean up your code.
  11. Can't figure it out...

    Is it possible then to create instances of book types similar to an array? Such as: Book bk(1) = new Book; What I am trying to do is create instances of the book type in a loop, so I would create a new instance with a counter, i. Is this possible and how would I go about it? Dan
  12. Can't figure it out...

    This is probably really really easy. I have a class, the code is: [CODE] #include <string> #include <iostream> #include <fstream> using namespace std; class NewReference { public: NewReference() { } virtual ~NewReference() { } virtual void CreateNewReference() const = 0; //virtual void DrawFill() const = 0; }; class Book : public NewReference { public: Book() { } virtual ~Book() { } virtual void CreateNewReference() const { cout << "TESTTESTEST" << endl; } }; [/CODE] How do I create a new newReference from another file? Eg. The class is Reference.h The file I am using is Main.cpp I have included Reference.h How do I call a new book reference? Any help is appreciated, Paintstripper88
  13. Reading a string from a text file

    I am trying to convert the raw data into a formatted output based on a referencing style. In this case it is APA 5th. Is it possible to do this without parser? All I have to do is search through the file and find the author for a specific entry, then store that entry to an 'author' variable.
  • Advertisement