Menu

Bug with member functions?

2002-10-24
2012-09-26
  • Nobody/Anonymous

    When I compile my project I get 23 errors, all concerning the member functions that have their definitions #included.

    Yet if I ignore the project, and simply load the MAIN and compile that, the program compiles and executes fine.

    Is this a known problem? I'm using Dev-C++ 4.01 with minGW2.95.

     
    • Nobody/Anonymous

      ???

      Someone might be able to help it you post some code.

      :-)

       
    • Nobody/Anonymous

      Thanks for the reply.

      I would have posted code, but : (a) It is a bit long, and,  (b) The fact I can get it to compile with Dev-C++, and also with Borland 5.5 suggests that it is a problem with Dev-C++ rather than the code.

      But here is the class:

      struct Tnode
      {
             string word;
             int count;
             int taken;
             Tnode* parent;
             Tnode* branches[26];
      };
                    

      class alphatree
      {
            public:
                     alphatree();
                    
                     Tnode* begin();
                     Tnode* end();
                     void erase(string&);
                     Tnode* get(string&);
                     void put (string&);
                     int size();
            private:
                     Tnode* root;
                     int nodecount;
                     Tnode* firstnode;
                    
                     Tnode* CreateNode(string, int, Tnode*);
      };            

      alphatree::alphatree()
      {
                           root = CreateNode("", 0, 0);
                           nodecount = 0;
                           firstnode = 0;
      }

      Tnode* alphatree::begin()
      {
             return firstnode;
      }

      Tnode* alphatree::end()
      {
             return root;
      }

      int alphatree::size()
      {
                        return nodecount;
      }

      #include "CreateNode - alphatree.cpp"
      #include "erase - alphatree.cpp"
      #include "get - alphatree.cpp"
      #include "put - alphatree.cpp"

      and here the member functions:

      Tnode* alphatree::CreateNode(string thisword, int count, Tnode* parent)
      {
             Tnode* temp = new struct Tnode;
             temp->word = thisword;
             temp->count = count;
             temp->taken = -1;
             temp->parent = parent;
             for (int i= 0; i< 26; i++)
             {
                 temp->branches[i] = 0;
             }
            
             if (parent != 0)
             {
                parent->branches[(thisword[thisword.length()-1])-97] = temp;
             }
            
      return temp;
      }

      Tnode* alphatree::get(string& thisword)
      {     
             Tnode* tn = root;
             int charpos = -1;
             while (true)
             {
                 if (thisword == tn->word)
                 {
                    if (tn->count != 0)
                    {
                       return tn;
                    }
                   
                     return 0;
                 }
                
                 charpos++;
                
                 if (tn->branches[thisword[charpos]-97] != 0)
                 {
                    tn = tn->branches[thisword[charpos]-97];
                    continue;
                 }
                
                 return 0;
             }
      }

      void alphatree::put (string& thisword)
      {
           Tnode* tn = root;
           int charpos = -1;
           while (true)
           {
                
                 if (thisword == tn->word)
                 {
                    tn->count++;
                    return;
                 }
                
                 charpos++;
                
                 if (tn->branches[thisword[charpos]-97] != 0)
                 {
                    tn = tn->branches[thisword[charpos]-97];
                    continue;
                 }
                
                 Tnode* temp = tn;
                 for (charpos; charpos< thisword.length()-1; charpos++)
                 {
                     temp = CreateNode(thisword.substr(0, charpos+1), 0, temp);
                 }
                 temp = CreateNode(thisword, 1, temp);
                 nodecount++;
                 if (firstnode == 0)
                 {
                    firstnode = temp;
                 }
                 else
                 {
                     if (thisword < firstnode->word)
                     {
                        firstnode = temp;
                     }
                 }
                 return;
           }
      }

      Attempting to compile this as a project produces these errors:

      1 createnode - alphatree.cpp
      syntax error before `*'

      4 createnode - alphatree.cpp
      syntax error before `->'

      5 createnode - alphatree.cpp
      syntax error before `->'

      6 createnode - alphatree.cpp
      syntax error before `->'

      7 createnode - alphatree.cpp
      syntax error before `->'

      8 createnode - alphatree.cpp
      parse error before `;'

      8 createnode - alphatree.cpp
      syntax error before `++'

      1 get - alphatree.cpp
      syntax error before `*'

      5 get - alphatree.cpp
      parse error before `while'

      17 get - alphatree.cpp
      syntax error before `++'

      1 put - alphatree.cpp
      syntax error before `::'

      5 put - alphatree.cpp
      parse error before `while'

      14 put - alphatree.cpp
      syntax error before `++'

      22 put - alphatree.cpp
      syntax error before `*'

      23 put - alphatree.cpp
      `thisword' was not declared in this scope

      23 put - alphatree.cpp
      parse error before `;'

      23 put - alphatree.cpp
      syntax error before `++'

      27 put - alphatree.cpp
      ANSI C++ forbids declaration `temp' with no type

      27 put - alphatree.cpp
      `thisword' was not declared in this scope

      27 put - alphatree.cpp
      implicit declaration of function `int CreateNode(...)'

      28 put - alphatree.cpp
      syntax error before `++'

      1 erase - alphatree.cpp
      syntax error before `::'

      4 erase - alphatree.cpp
      syntax error before `*'

      6 erase - alphatree.cpp
      parse error before `while'

      10 erase - alphatree.cpp
      parse error before `;'

      10 erase - alphatree.cpp
      syntax error before `++'

      15 erase - alphatree.cpp
      syntax error before `--'

      20 erase - alphatree.cpp
      syntax error before `*'

      21 erase - alphatree.cpp
      syntax error before `->'

      23 erase - alphatree.cpp
      syntax error before `--'

      27 erase - alphatree.cpp
      syntax error before `++'

      Thanks.

       
    • Nobody/Anonymous

      ___If you compile a Project with more than one source-files, it's wrong to INCLUDE the definitions of memberfunctions into another file. The compiler automaticly looks for definitions in every sourcefile the project contains.

      ___Maybe you should do all definitions in ONE file, not in 4 or 5 files!

      ___#include should get files with the ending *.h (like "stdio.h") or no ending (like "cstdlib"), altough it's possible to include maybe *.html or *.zip :)

      ___Maybe more comments, huh?

      ___What are the files you included from the std-library?? Is there the type "strings" defined??

      ___Don't code difficult without comments like:
      parent->branches[(thisword[thisword.length()-1])-97] = temp;

      I hope to help you with these things. let me know your success!?!?

      Tom

       
    • Nobody/Anonymous

      Thanks for the reply Tom.

      Put all the definitions in one file? What is the point of having an editor that handles multiple files?

      I have to say dividing up programs is something that doesnt get enough coverage in C++ books.

      I have to say though, that Visual C++ will accept my code as a project  workspace to it  and build without errors. Though as that is an introductory Edition that I got free with a book, I dont really want to use it as my main editor/compiler: it has no optimization, and puts up that bloody annoying message saying that you can not use it to create redistributable programs.

      As to your remarks about comments: youre mostly right. Though as Im learning C++ generally I am writing trivial programs. This may be an exception though: if I ever need to store a large number of words with fast access, I will return to this program.

       
    • Nobody/Anonymous

      Further to my immediately preceding post:

      from MAIN:

      #include <cstdlib>
      #include <iostream>
      #include <string>

       
    • Nobody/Anonymous

      Yes :: put all definitions of ONE class in a separate sourcefile. for Alphatree class you could (should)  write the DECLARATION in an Header File  (ATree.h) and all the DEFINITIONS of the member functions in an other file (ATreeDef.cpp) .

      Use the ressources on the Web to learn about special things like splitting a project into some sourcefiles. Universities have a lot of c++-proferts online, and there are uncountable (more or less) good tutorials.

      I feel the same way about MS VC++. Dev-C++ is free and more comfortable and has not so huge memory needs. But dev-C++ compiles DOS-programs that only run under Windows , not under DOS   :(

      Tom

       
    • Nobody/Anonymous

      (If my preceding post made it - ignore this)

      Hallelujah! It's working!

      Thanks for your help.

      Though for a more complete answer it's not necessary for all the source code to go into a single file: I've got some of the code directly in the 'root' source file, and some #included in it.

      I think the key point for me was that you include the header in the source, not the other way around!
      (yes I had that!)

      I got the final answer from Thinking in C++, 2nd ed. volume 1 which I had downloaded some time previously.

      If I ever know C++ well enough to write my own tutorial (!) I'll definitely cover this in more detail than most books do.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.