header file/class problems

This is a discussion on header file/class problems within the C++ Programming forums, part of the General Programming Boards category; Hi, I recently wrote a small morse code converter and then decided to combine it with the network programming stuff ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72

    header file/class problems

    Hi,

    I recently wrote a small morse code converter and then decided to combine it with the network programming stuff I'm learning and then decided to mash that up in turn with some OO programming which I am also learning and it would all run in linux. Not that I'm trying to overload myself or anything :P Anyways, I wrote a class, which is shown below, along with a few member functions which facilitate my server/client communication and stuck it all in a header file. Problem is the compiler always complains that it can't find the header file. I tried putting the header file in the compiler's search path, changing the suffix from '.h' to '.hpp' (which it should have been in the first place right?) and tried enclosing it in quotes i.e (#include<"morse.hpp">) alas nothing works. So I checked google but the various tutorials I read didn't mention anything I could see as protentially causing my problem. As far as I can tell I am doing everything right, but this is the first time I am using header files, and OOP so I guess I can blame that :P.

    It probably doesn't help that I am trying to compile this on linux live cds and on NetBSD running on a sparc64 processor lol but it should work right?

    Anyway here are the errors and code:

    -bash-2.05b$ g++ morse_serv.cpp -o morse.serv
    morse_serv.cpp:4:22: "morse.hpp": No such file or directory
    morse_serv.cpp: In function `int main(int, char**)':
    morse_serv.cpp:27: error: `morse' undeclared (first use this function)
    morse_serv.cpp:27: error: (Each undeclared identifier is reported only once for each function it appears in.)
    morse_serv.cpp:27: error: parse error before `;' token
    morse_serv.cpp:30: error: invalid conversion from `char*' to `short unsigned int'
    morse_serv.cpp:48: error: invalid conversion from `long unsigned int' to `__socklen_t*'
    morse_serv.cpp:54: error: conversion from `__in_addr_t' to non-scalar type `in_addr' requested
    morse_serv.cpp:75: error: `m' undeclared (first use this function)
    morse_serv.cpp:75: error: parse error before `]' token
    Now I'm concentrating on the class and header file releated errors as I am pretty sure I have had the others before and they are fairly easy to fix. Btw, this is the output from g++ on netbsd^^.

    Code:
    #include<iostream>
    #include<cstdlib>
    
    class morse
    {
    	public:
    		char lookup[28] = { 'a' ,'b' ,'c' ,'d' ,'e' ,'f' ,'g' ,'h' ,'i' ,'j' ,'k' ,'l' ,'m' ,'n' ,'o' ,'p' ,'q' ,'r' ,'s' ,'t' ,'u' ,'v' ,'w' ,'x' ,'y' ,'z' ,'.' ,',' };		
    		char morse[28][6] = {{ '.','-'},			//a                  The code tags seem to 
    			        		{ '-','.','.','.'},		//b                         be screwing with my 
    			        		{ '-','.','-','.'},		//c                         indentation....sorry
    			        		{ '-','.','.'},			//d                        guys!
    						{ '.'},					//e
    						{ '.','.','-','.'},		//f
    						{ '-','-','.'},			//g
    						{ '.','.','.','.'},		//h
    						{ '.','.'},				//i
    						{ '.','-','-','-'},		//j
    						{ '-','.','-'},			//k
    						{ '.','-','.','.'},		//l
    						{ '-','-'},				//m
    						{ '-','.'},				//n
    						{ '-','-','-'},			//o
    						{ '.','-','-','.'},		//p
    						{ '-','-','.','-'},		//q
    						{ '.','-','.'},			//r
    						{ '.','.','.'},			//s
    						{ '-'},					//t
    						{ '.','.','-'},			//u
    						{ '.','.','.','-'},		//v
    						{ '.','-','-'},			//w
    						{ '-','.','.','-'},		//x
    						{ '-','.','-','-'},		//y
    						{ '-','-','.','.'},		//z
    						{ '.','-','.','-','.','-'},		//.
    						{ '-','-','.','.','-','-'}		//,
    						};
    		void ctob( char input[] )
    		{//open func
    			int input_len = strlen(input);
    			if ( input[0] == '\0' )
    			{
    				cout<<"No input given, exiting...";
    				exit(1);
    			}
    			for ( int i = 0; i < input_len; i++ )
    			{
    				for ( int j = 0; j < 28; j++ )
    				{
    					if ( input[i] == lookup[j] )
    					{
    						cout<< morse[j] <<"\n";
    						int k = 0;
    						while ( morse[j] [k] != '\0' )
    						{
    							if ( morse [j] [k] == '.' )
    							{
    								system("beep -f 700 -l 100");
    								system("sleep 0.17 s");
    							}
    							else if ( morse [j] [k] == '-' )
    							{
    								system("beep -f 700 -l 100");
    								system("sleep 0.17 s");
    							}
    							k++;
    						}
    						system("sleep 0.17 s");
    						break;
    					}
    				}
    			}
    		}//close func
    		void mtob(char input[])
    		{//open func
    			int l = 0;
    			while ( input[l] != '\0' )
    			{
    				if ( input[l] == '.' )
    				{
    					system("beep -f 700 -l 100");
    					system("sleep 0.17 s");
    				}
    				else if ( input[l] == '-' )
    				{
    					system("beep -f 700 -l 100");
    					system("sleep 0.17 s");
    				}
    				l++;
    				cout<< input[i];
    			}
    			system("sleep 0.17 s");
    		}//close func
    		char ctom(char input[])
    		{//open func
    			int ctom_len = strlen(input);
    			for ( int i = 0; i < ctom_len; i++ )
    			{
    				for ( int j = 0; j < 28; j++ )
    				{
    					if ( input[i] == lookup[j] )
    					{
    						char morse_code[] = morse[j];
    					}
    				}
    			}
    		}//close func
    };//close class
    I have included those header files as the compiler complained otherwise.

    Code:
    #include<iostream>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<"morse.hpp">
    #include<netinet/in.h>
    #include<arpa/inet.h>
    
    #define CLIENT_QUEUE_SIZE 2
    #define MAX_DATA 1024
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	if ( argc == 3 )
    	{
    		cout<<"USAGE: ./morse_list [PORT NUMBER]\n";
    		exit(-1);
    	}
    	int sock, cli_sock;
    	char *greeting = "Connected to the server.\n";
    	char input[MAX_DATA];
    	int len = strlen(greeting);
    	int recv_len;
    	struct sockaddr_in serv;
    	struct sockaddr_in client;
    	morse m;
    
    	serv.sin_family = AF_INET;
    	serv.sin_port = htons(argv[1]);
    	serv.sin_addr.s_addr = INADDR_ANY;
    	memset(serv.sin_zero, '\0', sizeof(serv.sin_zero));
    	if ((sock = socket( PF_INET, SOCK_STREAM, 0 )) == -1 )
    	{
    		cout<<"Could not open socket.\n";
    		exit(-1);
    	}
    	if ((bind( sock, ( struct sockaddr *)&serv, sizeof(serv))) == -1 )
    	{
    		cout<<"Could not bind socket.\n";
    		exit(-1);
    	}
    	if ((listen( sock, CLIENT_QUEUE_SIZE )) == -1 )
    	{
    		cout<<"Could not listenon the network.\n";
    		exit(-1);
    	}
    	cli_sock = accept( sock, (struct sockaddr *)&client, sizeof(client));
    	if ( cli_sock == -1 )
    	{
    		cout<<"Could not accept connection.\n";
    		exit(-1);
    	}
    	cout<<"Connection on port "<<ntohs(client.sin_port)<<"from "<<inet_ntoa(client.sin_addr.s_addr)<<"\n";	
    	int bytes_sent = send( cli_sock, greeting, len, 0 );
    	if ( bytes_sent == -1 )
    	{
    		cout<<"FAIL: Could not send greeting\n";
    	}
    	while(1)
    	{
    		memset(input, '\0', MAX_DATA);
    		if ( (recv( cli_sock, input, MAX_DATA, 0 )) == -1 )
    		{
    			cout<<"Failed to receive data.\n";
    			exit(-1);
    		}
    		recv_len=strlen(input);
    		if ( input[0] == '\0' )
    		{
    			cout<<"Empty packet received, waiting for next useful packet...";
    		}
    		else
    		{
    			m.mtob(input[]);
    		}
    	}
    	close(cli_sock);
    	cout<<"Client connection terminated.\n";
    	close(sock);
    }
    Thanks in advance,

    Calef13

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    What's in the file doesn't matter until you fix this
    > morse_serv.cpp:4:22: "morse.hpp": No such file or directory
    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.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> #include<"morse.hpp">

    It's either brackets or quotes, not both. For user files like this one use quotes.

    Whether it is hpp or h depends on whether you named the file hpp or h. Either way is fine, just be consistent.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    Thanks it's finding the morse.hpp file now, but it's now throwing up loads of other errors, I'll take a look and post back if I have any more problems.

    Thanks,

    Calef13

  5. #5
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    ok I have a few questions. Is the exit() ISO C++ or should I use return 1;? The same question for strlen() too. The problem is that the compiler throws up the following error when I use return in the function which makes sense so is there a c++ version of exit or should I just use exit()?

    Code:
    morse.hpp: In member function `void morse::ctob(char*)':
    morse.hpp:40: error: return-statement with a value, in function declared with a
       void return type
    I took the #include lines out of the header file, am I right in thinking it's a bad idea to include common header files like iostream in header files as you are then probably going to include them twice?

    The following errors are baffling me; am I not allowed to initialize the elements of an array on declaration within a class or something?

    Code:
    In file included from morse_serv.cpp:4:
    morse.hpp:5: error: ISO C++ forbids initialization of member `lookup'
    morse.hpp:5: error: making `lookup' static
    morse.hpp:5: error: invalid in-class initialization of static data member of
       non-integral type `char[28]'
    morse.hpp:33: error: ISO C++ forbids initialization of member `morse'
    morse.hpp:33: error: making `morse' static
    morse.hpp:33: error: invalid in-class initialization of static data member of
       non-integral type `char[28][6]'
    Here is the morse.hpp file for reference:

    Code:
    class morse
    {
            public:
                    char lookup[28] = { 'a' ,'b' ,'c' ,'d' ,'e' ,'f' ,'g' ,'h' ,'i' ,'j' ,'k' ,'l' ,'m' ,'n' ,'o' ,'p' ,'q' ,'r' ,'s' ,'t' ,'u' ,'v' ,'w' ,'x' ,'y' ,'z' ,'.' ,',' };                               
                    char morse[28][6] = {{ '.','-'},                        //a
                                                    { '-','.','.','.'},             //b
                                                    { '-','.','-','.'},             //c
                                                    { '-','.','.'},                 //d
                                                            { '.'},                                 //e
                                                            { '.','.','-','.'},             //f
                                                            { '-','-','.'},                 //g
                                                            { '.','.','.','.'},             //h
                                                            { '.','.'},                             //i
                                                            { '.','-','-','-'},             //j
                                                            { '-','.','-'},                 //k
                                                            { '.','-','.','.'},             //l
                                                            { '-','-'},                             //m
                                                            { '-','.'},                             //n
                                                            { '-','-','-'},                 //o
                                                            { '.','-','-','.'},             //p
                                                            { '-','-','.','-'},             //q
                                                            { '.','-','.'},                 //r
                                                            { '.','.','.'},                 //s
                                                            { '-'},                                 //t
                                                            { '.','.','-'},                 //u
                                                            { '.','.','.','-'},             //v
                                                            { '.','-','-'},                 //w
                                                            { '-','.','.','-'},             //x
                                                            { '-','.','-','-'},             //y
                                                            { '-','-','.','.'},             //z
                                                            { '.','-','.','-','.','-'},             //.
                                                            { '-','-','.','.','-','-'}              //,
                                                            };
                    void ctob( char input[] )
                    {//open func
                            int input_len = strlen(input);
                            if ( input[0] == '\0' )
                            {
                                    std::cout<<"No input given, exiting...";
                                    return 1;
                            }
                            for ( int i = 0; i < input_len; i++ )
                            {
                                    for ( int j = 0; j < 28; j++ )
                                    {
                                            if ( input[i] == lookup[j] )
                                            {
                                                    std::cout<< morse[j] <<"\n";
                                                    int k = 0;
                                                    while ( morse[j] [k] != '\0' )
                                                    {
                                                            if ( morse [j] [k] == '.' )
                                                            {
                                                                    system("beep -f 700 -l 100");
                                                                    system("sleep 0.17 s");
                                                            }
                                                            else if ( morse [j] [k] == '-' )
                                                            {
                                                                    system("beep -f 700 -l 100");
                                                                    system("sleep 0.17 s");
                                                            }
                                                            k++;
                                                    }
                                                    system("sleep 0.17 s");
                                                    break;
                                            }
                                    }
                            }
                    }//close func
                    void mtob(char input[])
                    {//open func
                            int l = 0;
                            while ( input[l] != '\0' )
                            {
                                    if ( input[l] == '.' )
                                    {
                                            system("beep -f 700 -l 100");
                                            system("sleep 0.17 s");
                                    }
                                    else if ( input[l] == '-' )
                                    {
                                            system("beep -f 700 -l 100");
                                            system("sleep 0.17 s");
                                    }
                                    l++;
                                    std::cout<< input[i];
                            }
                            system("sleep 0.17 s");
                    }//close func
                    char ctom(char input[])
                    {//open func
                            int ctom_len = strlen(input);
                            for ( int i = 0; i < ctom_len; i++ )
                            {
                                    for ( int j = 0; j < 28; j++ )
                                    {
                                            if ( input[i] == lookup[j] )
                                            {
                                                    char morse_code[] = morse[j];
                                            }
                                    }
                            }
                    }//close func
    };//close class
    I think some of the other errors are relevant to my questions (like the fact that even though I declared the two arrays at the start they are flagged as undeclared by the compiler below) so the full list of errors is below:

    Code:
    -bash-2.05b$ g++ morse_serv.cpp -o server
    In file included from morse_serv.cpp:4:
    morse.hpp:5: error: ISO C++ forbids initialization of member `lookup'
    morse.hpp:5: error: making `lookup' static
    morse.hpp:5: error: invalid in-class initialization of static data member of
       non-integral type `char[28]'
    morse.hpp:33: error: ISO C++ forbids initialization of member `morse'
    morse.hpp:33: error: making `morse' static
    morse.hpp:33: error: invalid in-class initialization of static data member of
       non-integral type `char[28][6]'
    morse.hpp: In member function `void morse::ctob(char*)':
    morse.hpp:40: error: return-statement with a value, in function declared with a
       void return type
    morse.hpp:46: error: `lookup' undeclared (first use this function)
    morse.hpp:46: error: (Each undeclared identifier is reported only once for each
       function it appears in.)
    morse.hpp:48: error: parse error before `;' token
    morse.hpp:50: error: parse error before `;' token
    morse.hpp:57: error: parse error before `;' token
    morse.hpp:62: error: `k' undeclared (first use this function)
    morse.hpp: At global scope:
    morse.hpp:67: error: parse error at end of saved function text
    morse.hpp: In member function `void morse::mtob(char*)':
    morse.hpp:86: error: `i' undeclared (first use this function)
    morse.hpp: In member function `char morse::ctom(char*)':
    morse.hpp:99: error: parse error before `;' token
    morse.hpp:99: error: storage size of `morse_code' isn't known
    morse_serv.cpp: In function `int main(int, char**)':
    morse_serv.cpp:30: error: invalid conversion from `char*' to `short unsigned
       int'
    morse_serv.cpp:48: error: invalid conversion from `long unsigned int' to `
       __socklen_t*'
    morse_serv.cpp:54: error: conversion from `__in_addr_t' to non-scalar type `
       in_addr' requested
    morse_serv.cpp:75: error: parse error before `]' token
    If anyone needs it the morse_serv.cpp file is still the same as above, but I want to debug the morse.hpp file first the errors in morse_serv.cpp are not really priority at the moment as I am finding morse.hpp to be more interesting ;-).

    Thanks in advance,

    Calef13

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    A development process
    Erm, how about delete all the code and start again?

    It's obvious that you wrote the entire thing at one sitting and now you have 1000's of errors which you're swamped with dealing with. Dumping the whole lot onto a message board for some other schmoe to fix doesn't work either.

    OK, lets say you have
    Code:
    void foo ( int a ) {
      int i;
      for ( i = 0 ; i < a ; i++ ) {
        cout << i;
      }
    }
    The first step is to do this to all your functions
    Code:
    void foo ( int a ) {
    #if 0
      int i;
      for ( i = 0 ; i < a ; i++ ) {
        cout << i;
      }
    #endif
    }
    It's the same as deleting code (as far as the compiler is concerned). So in the first instance, you have a bunch of apparently empty functions.
    Press COMPILE and FIX any problems before doing anything else. Like for example making sure prototypes and definitions match up.

    Then you move the conditionals closer together to expose more code, but only a few lines at a time so that you don't get so many problems in one hit.
    Code:
    void foo ( int a ) {
      int i;
      for ( i = 0 ; i < a ; i++ ) {
    #if 0
        cout << i;
    #endif
      }
    }
    So long as the braces continue to match, then all should be fine.

    Repeat that process until you have nothing between any of the #if 0/#endif pairs.
    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.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    Sorry, you are absolutely right, I usually write code in one go which works for small stuff but obviously not this time. I should really read over OOP and header files more. Thanks for the advice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-14-2005, 01:21 PM
  2. Problems in reading binary file
    By serena in forum C Programming
    Replies: 3
    Last Post: 04-14-2005, 03:54 AM
  3. I keep seeing some advice.. (Header file related)
    By Lithorien in forum C++ Programming
    Replies: 10
    Last Post: 01-14-2005, 03:31 AM
  4. Replies: 6
    Last Post: 04-02-2002, 04:46 AM
  5. header file compile error
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 02-23-2002, 05:28 AM

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