Visit Our UNIX and Linux User Community


Implementing function outside struct


 
Thread Tools Search this Thread
Top Forums Programming Implementing function outside struct
# 1  
Old 02-15-2012
Implementing function outside struct

I have this code where I have declared a struct with some functions. Trying to write the function implementation outside the struct declaration and do not know how to proceed.

Code:
#ifndef ParseEl_hh
#define ParseEl_hh

#include <iostream>
#include <fstream>

#include "DynBaseObj.hh"
#include "List.hh"
#include "Stack.hh"
#include "Tree.hh"
#include "Vect2.hh"
#include "Vector.hh"
#include "String.hh"
#include "common.hh"

enum TypeParse { PARAM, LIST };

////////////////////////////////////////////////////////////////////////////////////////////////////

struct ParseEl {

  enum TypeParse Type;
  int  Hash;
  String  Str;
  String  Name;
  List<String>  StringList;
  List<int>  Ord;

  ParseEl() { }

  ParseEl(
    const String&  name,
    String&  s,
    const List<int>&  ord) {

    Name = name;
    Str = s;
    Type = PARAM;
    Ord = ord;
    Hash = Name.Hash();

    while ((Ord.size() > 0) && (Ord[Ord.size()-1] == 0)) {
        Ord.Delete(Ord.size() - 1);
    }

  }

////////////////////////////////////////////////////////////////////////////////////////////////////

  ParseEl(
    const String&  name,
    const List<String>&  ls,
    const List<int>&  ord) {

    Name = name;
    StringList = ls;
    Type = LIST;
    Ord = ord;
    Hash = Name.Hash();

    while ((Ord.size() > 0) && (Ord[Ord.size()-1] == 0)) {
      Ord.Delete(Ord.size() - 1);
    }

  }

  ParseEl(const ParseEl&  parsel ) {

    Name = parsel.Name;
    Type = parsel.Type;
    Str = parsel.Str;
    StringList = parsel.StringList;
    Ord = parsel.Ord;
    Hash = parsel.Hash;

  }

  friend  int  operator == (
      const ParseEl  p1,
      const ParseEl  p2) {

      return (    (p1.Hash == p2.Hash) && (p1.Type == p2.Type)
          && (p1.Name == p2.Name)  && (p1.Ord == p2.Ord)
          && (p1.Str == p2.Str) && (p1.StringList == p2.StringList) );

  }

};

#endif

---------- Post updated at 09:08 AM ---------- Previous update was at 06:46 AM ----------

I have now changed the code and things are working fine. My last problem is how to take care of ParseEl() { } to be outside the struct. It is not doing anything so I suppose I can just declare it inside the struct as ParseEl();

Code:
#ifndef ParseEl_hh
#define ParseEl_hh

#include <iostream>
#include <fstream>

#include "DynBaseObj.hh"
#include "List.hh"
#include "Stack.hh"
#include "Tree.hh"
#include "Vect2.hh"
#include "Vector.hh"
#include "String.hh"
#include "common.hh"

enum TypeParse { PARAM, LIST };

////////////////////////////////////////////////////////////////////////////////////////////////////

struct ParseEl {

// -- Attributes -----------------------------------------------------------------------------------

  enum TypeParse Type;
  int  Hash;
  String  Str;
  String  Name;
  List<String>  StringList;
  List<int>  Ord;

// -- Operations -----------------------------------------------------------------------------------

  ParseEl() { }

  ParseEl(
    const String&  name,
    String&  s,
    const List<int>&  ord);

  ParseEl(
    const String&  name,
    const List<String>&  ls,
    const List<int>&  ord);

  ParseEl(
    const ParseEl&  parsel);

  friend int operator  == (
    const ParseEl  p1,
    const ParseEl  p2);

};

////////////////////////////////////////////////////////////////////////////////////////////////////

  ParseEl::ParseEl(
    const String&  name,
    String&  s,
    const List<int>&  ord) {

    Name = name;
    Str = s;
    Type = PARAM;
    Ord = ord;
    Hash = Name.Hash();

    while ((Ord.size() > 0) && (Ord[Ord.size()-1] == 0)) {
        Ord.Delete(Ord.size() - 1);
    }

  }

////////////////////////////////////////////////////////////////////////////////////////////////////

  ParseEl::ParseEl(
    const String&  name,
    const List<String>&  ls,
    const List<int>&  ord) {

    Name = name;
    StringList = ls;
    Type = LIST;
    Ord = ord;
    Hash = Name.Hash();

    while ((Ord.size() > 0) && (Ord[Ord.size()-1] == 0)) {
      Ord.Delete(Ord.size() - 1);
    }

  }

////////////////////////////////////////////////////////////////////////////////////////////////////

  ParseEl::ParseEl(
    const ParseEl&  parsel) {

    Name = parsel.Name;
    Type = parsel.Type;
    Str = parsel.Str;
    StringList = parsel.StringList;
    Ord = parsel.Ord;
    Hash = parsel.Hash;

  }

////////////////////////////////////////////////////////////////////////////////////////////////////

  int  operator == (
    const ParseEl  p1,
    const ParseEl  p2) {

    return (    (p1.Hash == p2.Hash) && (p1.Type == p2.Type) && (p1.Name == p2.Name)
             && (p1.Ord == p2.Ord) && (p1.Str == p2.Str) && (p1.StringList == p2.StringList) );

  }

#endif

# 2  
Old 02-15-2012
What you want: function pointers. Create a function pointer as an element of the struct.


The Function Pointer Tutorials - Index
# 3  
Old 02-15-2012
Currently ParseEl is being used by class Parsing and called as follows:
Code:
Parsing.hh:#include "ParseEl.hh"
Parsing.hh:  List<ParseEl>  Index;                //
Parsing.hh:              Index += ParseEl(CompleteName, Ends, Ord);
Parsing.hh:              Index += ParseEl(CompleteName, L, Ord);
Parsing.hh:          Index += ParseEl(Name, Ends, Ord);

Also I use it as a private member in class Parsing as follows:

Code:
List<ParseEl>  Index;

And in its own class such as

Code:
const ParseEl  p1

Currently I just want the same functionality, basically leaving
everything as it is, however do the declarations inside the class
definition and all implementations outside.
# 4  
Old 02-15-2012
None of your implementations are inside the structure anymore, which will allow you to remove them from the header file and put them in a separate source file. Compile and link this source file in with the rest of your project, and the symbols should be found.

Code:
#ifndef ParseEl_hh
#define ParseEl_hh

#include <iostream>
#include <fstream>

#include "DynBaseObj.hh"
#include "List.hh"
#include "Stack.hh"
#include "Tree.hh"
#include "Vect2.hh"
#include "Vector.hh"
#include "String.hh"
#include "common.hh"

enum TypeParse { PARAM, LIST };

////////////////////////////////////////////////////////////////////////////////////////////////////

struct ParseEl {

// -- Attributes -----------------------------------------------------------------------------------

  enum TypeParse Type;
  int  Hash;
  String  Str;
  String  Name;
  List<String>  StringList;
  List<int>  Ord;

// -- Operations -----------------------------------------------------------------------------------

  ParseEl() { }

  ParseEl(
    const String&  name,
    String&  s,
    const List<int>&  ord);

  ParseEl(
    const String&  name,
    const List<String>&  ls,
    const List<int>&  ord);

  ParseEl(
    const ParseEl&  parsel);

  friend int operator  == (
    const ParseEl  p1,
    const ParseEl  p2);

};

#endif

Code:
//parseel.cpp

#include "parseel.h"

////////////////////////////////////////////////////////////////////////////////////////////////////

  ParseEl::ParseEl(
    const String&  name,
    String&  s,
    const List<int>&  ord) {

    Name = name;
    Str = s;
    Type = PARAM;
    Ord = ord;
    Hash = Name.Hash();

    while ((Ord.size() > 0) && (Ord[Ord.size()-1] == 0)) {
        Ord.Delete(Ord.size() - 1);
    }

  }

////////////////////////////////////////////////////////////////////////////////////////////////////

  ParseEl::ParseEl(
    const String&  name,
    const List<String>&  ls,
    const List<int>&  ord) {

    Name = name;
    StringList = ls;
    Type = LIST;
    Ord = ord;
    Hash = Name.Hash();

    while ((Ord.size() > 0) && (Ord[Ord.size()-1] == 0)) {
      Ord.Delete(Ord.size() - 1);
    }

  }

////////////////////////////////////////////////////////////////////////////////////////////////////

  ParseEl::ParseEl(
    const ParseEl&  parsel) {

    Name = parsel.Name;
    Type = parsel.Type;
    Str = parsel.Str;
    StringList = parsel.StringList;
    Ord = parsel.Ord;
    Hash = parsel.Hash;

  }

////////////////////////////////////////////////////////////////////////////////////////////////////

  int  operator == (
    const ParseEl  p1,
    const ParseEl  p2) {

    return (    (p1.Hash == p2.Hash) && (p1.Type == p2.Type) && (p1.Name == p2.Name)
             && (p1.Ord == p2.Ord) && (p1.Str == p2.Str) && (p1.StringList == p2.StringList) );

  }

---------- Post updated at 11:06 AM ---------- Previous update was at 11:04 AM ----------

Also, you really should make your comparison operator pass by reference! Duplicating two lists whenever you want to just compare them will not be efficient!

Code:
  friend int operator  == (
    const ParseEl  &p1,
    const ParseEl  &p2);

Code:
  int  operator == (
    const ParseEl  &p1,
    const ParseEl  &p2) {

    return (    (p1.Hash == p2.Hash) && (p1.Type == p2.Type) && (p1.Name == p2.Name)
             && (p1.Ord == p2.Ord) && (p1.Str == p2.Str) && (p1.StringList == p2.StringList) );

  }

# 5  
Old 02-15-2012
That's correct. My question is what shall I do about

Code:
 ParseEl() { }

# 6  
Old 02-15-2012
You could either inline it:

Code:
...

inline ParseEl() { }

...

Or do the same thing with it you did with everything else:

Code:
ParseEl();

Code:
ParseEl::ParseEl() { }

You should put something in your constructor anyway. You're leaving your Type and Hash variables unset, hence at values which only might, usually be zero...
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 02-15-2012
I was thinking of letting the compiler take care of the constructor, thus removing the line. Any opinion about this?

Previous Thread | Next Thread
Test Your Knowledge in Computers #273
Difficulty: Easy
In December 1939, Turing solved the essential part of the naval indicator system, which was easier and less complex than the indicator systems used by the other services.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Implementing Listagg like function in shell

Hi, Basically what I am trying to do is making multiple fields of the same type comma-separated. i.e. for a data like this: B00000 abc B00001 abc,def B00001 ghi B00001 jkl B00002 abc B00002 def B00003 xyz Output should be like: B00000 abc B00001 abc,def,ghi,jkl... (20 Replies)
Discussion started by: prohank
20 Replies

2. Programming

Storing C++-struct in file - problem when adding new item in struct

Hi, I have received an application that stores some properties in a file. The existing struct looks like this: struct TData { UINT uSizeIncludingStrings; // copy of Telnet data struct UINT uSize; // basic properties: TCHAR szHost; //defined in Sshconfig UINT iPortNr; TCHAR... (2 Replies)
Discussion started by: Powerponken
2 Replies

3. Programming

Problem with implementing the times() function in C (struct tms times return zero/negative values)

Hello, i'm trying to implement the times() function and i'm programming in C. I'm using the "struct tms" structure which consists of the fields: The tms_utime structure member is the CPU time charged for the execution of user instructions of the calling process. The tms_stime structure... (1 Reply)
Discussion started by: g_p
1 Replies

4. Shell Programming and Scripting

converting from c struct to function

Hey Guys, I need your help where I have a C structure and I want it to be converted into corresponding function. Example: typedef struct { unsigned long LineNum; //1025-4032 unsigned short KeyNum; /*tbd*/ char Key; /*between 1-3*/... (1 Reply)
Discussion started by: skyos
1 Replies

5. Shell Programming and Scripting

Need help in implementing logic

i have following input file... 00290002STDR000000000000000000000000000EOD END TRANSACTION ^@^@^@^@^@^@^@^@^@^@^@^@^ 00299998STDR070000000007000000000000000STANDING DEBITS ^@^@^@^@^@^@^@^@^@^@^@^@^... (1 Reply)
Discussion started by: sagarrd
1 Replies

6. UNIX for Dummies Questions & Answers

How to access a struct within a struct?

Can someone tell me how to do this? Just a thought that entered my mind when learning about structs. First thought was: struct one { struct two; } struct two { three; } one->two->three would this be how you would access "three"? (1 Reply)
Discussion started by: unbelievable21
1 Replies

7. UNIX for Advanced & Expert Users

problem with netfilter hook function struct skbuff *sock is null..

iam trying to built a firewall.so i have used netfilter for it. in function main_hook sock_buff is returning null and in my log file continuously "sock buff null" is printed plse help to solve this problem.. (using print_string iam printing strings on current terminal (terminal we ping)) ... (1 Reply)
Discussion started by: pavan6754
1 Replies

8. Shell Programming and Scripting

Help with implementing logging

I'm trying to add logging to an existing script which echos a number of lines to the screen. I've added a switch to the script that is going to suppress much of this output and put it in a file instead. The way I envisioned it was like this: $log would be set to either "" or the log files... (8 Replies)
Discussion started by: cheetobandito
8 Replies

9. Shell Programming and Scripting

Implementing Password

I am trying to implement a login screen to the following code how would i go about doing so. I have try to place the password in a variable using if statements which would usually work but as i have the system in a while loop i think i need to find another method. #!/bin/bash #Filename:... (4 Replies)
Discussion started by: warlock129
4 Replies

10. Programming

Implementing the redirection

Hi all I am facing a problem with redirection. Its somewhat related to parsing. I am following the following steps. 1. take the command and tokenize it. 2. if redirection is there then give it to redirection unit 3. if pipe is there give it to piping unit. 4. do until the command ends ... (0 Replies)
Discussion started by: mobile01
0 Replies

Featured Tech Videos