View unanswered posts | View active topics It is currently Thu May 28, 2026 9:26 am



Reply to topic  [ 4 posts ] 
 C++ Question 
Author Message
Gameop
User avatar

Joined: Sun Oct 08, 2006 2:00 am
Posts: 991
Unread post 
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

_________________
"All warfare is based on deception..." - Art of War
"Time will tell all tales" - SG
Any advanced tactic in TW is indistinguishable from cheating.


Sun Jun 10, 2007 5:22 pm
Profile ICQ
Commander
User avatar

Joined: Fri Jun 09, 2006 2:00 am
Posts: 1402
Location: Canada
Unread post 
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.

_________________
----------------------------
-= QUANTUM Computing 101: 15 = 3 x 5 ... 48% of the time.


Sun Jun 10, 2007 5:29 pm
Profile ICQ YIM
Gameop
User avatar

Joined: Sun Oct 08, 2006 2:00 am
Posts: 991
Unread post 
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

_________________
"All warfare is based on deception..." - Art of War
"Time will tell all tales" - SG
Any advanced tactic in TW is indistinguishable from cheating.


Sun Jun 10, 2007 5:41 pm
Profile ICQ
Lieutenant J.G.

Joined: Fri Apr 05, 2002 3:00 am
Posts: 332
Location: USA
Unread post 
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.

_________________
Creator of the TWGS Data Access Library
http://twgs.xiuhtec.com


Sun Jun 10, 2007 6:33 pm
Profile ICQ YIM
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by wSTSoftware.