| www.ClassicTW.com https://mail.black-squirrel.com/ |
|
| C++ Question https://mail.black-squirrel.com/viewtopic.php?f=14&t=19460 |
Page 1 of 1 |
| Author: | Cerne [ Sun Jun 10, 2007 5:22 pm ] |
| Post subject: | |
I have a programming problem to solve. I need to use 2 .cpp files and 1 .h file Simplified.... main.cpp Code: #include "help.h" using namespace std; void enterToContinue(); int main(){ help.menuHelp(); enterToContinue(); return 1; } void enterToContinue() { cout << "Press [ENTER] to continue: "; cin; cin.ignore(); } help.h Code: #include <iostream.h> #include <fstream.h> //struct definitions struct helpItem{ char helpOne[21]; char helpTwo[21]; char helpThree[21]; char helpFour[21]; char helpFive[21]; char helpSix[21]; }; struct node{ helpItem myHelp; node * next; }; //class definition class help{ public: help(); ~help(); void menuHelp(); private: node * head; node * tail; int size; int num; char filename[21]; }; help.cpp Code: #include "help.h" help::menuHelp(){ cout << "Some Help Stuff! << emdl; } The error I get is: main.cpp expected primary-expression before '.' token The compile error occurs on this line: help.menuHelp(); ^____________the '.' token What am I doing wrong. Cerne |
|
| Author: | LoneStar [ Sun Jun 10, 2007 5:29 pm ] |
| Post subject: | |
Been many years since i've done anything in C/C++ ...but is the main cpp file in the project list at the top of the hierarchy? hope this helps. |
|
| Author: | Cerne [ Sun Jun 10, 2007 5:41 pm ] |
| Post subject: | |
LoneStar wrote: Been many years since i've done anything in C/C++ ...but is the main cpp file in the project list at the top of the hierarchy? hope this helps. The G++ compiler handles the linking so I am good there. I found the error though. Like an idiot I forgot to create an object of my class. help helpData; //create an object of the help class helpData.menuHelp(); ------------ Now the Darn thing compiles just fine and I want to shoot myself in the head. Cerne |
|
| Author: | Xentropy [ Sun Jun 10, 2007 6:33 pm ] |
| Post subject: | |
Two things. One, if a function in a class doesn't utilize any class member data, you can declare it static and access it via classname::function(), not needing to create an instance of the object. Doesn't look like it applies here, since your help object is populated with data for menuHelp to output, but something to keep in mind. Two, and I can't stress this more strongly, learn the STL. I see you using node pointers and the like. Implementing your own linked lists is a sure way to get yourself into trouble. You could implement everything you did there much more simply with the STL. Code: #include <iostream> #include <fstream> #include <list> class help { public: help(); ~help(); void menuHelp(); private: struct helpItem { char helpOne[21]; char helpTwo[21]; char helpThree[21]; char helpFour[21]; char helpFive[21]; char helpSix[21]; }; std::list<helpItem> helpItems; char filename[21]; }; Then in your constructor instead of following node pointers around to add/remove/read data, you just use STL list mechanics. Code: help::help() { std::ifstream inputFile; inputFile.open(filename, std::ios::in); while( /* still reading data; I assume this is in a file but however */ ) { helpItem newItem; // load data into newItem.helpOne through newItem.helpSix helpItems.push_back(newItem); } } std::list's have the advantage of being bidirectionally linked and you have the ability to use a lot of built-in capabilities found in <algorithm> on them, like sorting, searching, etc. You also don't have to worry about garbage collection, bad pointers, memory leaks, etc. You'd access the data via an iterator, which uses syntax a lot like a pointer but knows how to travel through the linked list properly, like so: Code: void help::menuHelp() { for (std::list<helpItem> helpIter = helpItems.begin(); // Gets an iterator to the start of the linked list helpIter != helpItems.end(); // Checks to see if we've driven off the end of the list ++helpIter) { // Moves to the next entry in the linked list cout << helpIter->helpOne << helpIter->helpTwo; // etc. /* Note how helpIter is using pointer semantics to access its members. item->subitem is the same as (*item).subitem */ } } I'd recommend using std::string's instead of C-strings (char arrays) too, but didn't want to confuse the example by introducing two concepts at once. Obviously not knowing exactly what you're doing, my examples are going to be very generic, but it saves a lot of work storing and traveling along messy pointers. You can find a lot of great tutorials on using the C++ STL online. |
|
| Page 1 of 1 | All times are UTC - 5 hours |
| Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |
|