Thread: LinkedList based abstract Datatype for Pairs and such.

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    5

    LinkedList based abstract Datatype for Pairs and such.

    Hi,

    coming from Java, my experience with the Classes in C++ is quite limited.
    Thats why I am having trouble converting the following (simple!) Java-Program.
    Most examples with linked lists I found on the web describe how to implement the LinkedList class itself. But my problem is different: I want to use such a Class (I have a LinkedList class available on my system which is presumably OK).

    I hope somebody can help me translate the Java-Code to C++-Code.

    Cheers!

    Code:
    package main;
    
    import java.util.ArrayList;
    
    public class Main {
    
        public static void main(String[] args) {
            BoxList<String> sl = new BoxList<String>(
                    new ArrayList<ABox<String>>());
            sl.getList().add(
                    new BoxPair2<String>(new BoxPair2<String>(
                            new BoxBasic<String>("s1"),
                            new BoxBasic<String>("s2")), new BoxPair2<String>(
                            new BoxBasic<String>("s3"),
                            new BoxBasic<String>("s4"))));
            System.out.println(sl.toString()); // (((s1,s2),(s3,s4)))
            sl.getList().set(0, new BoxBasic<String>("s5"));
            System.out.println(sl.toString()); // (s5)
        }
    }
    Code:
    package main;
    
    public abstract class ABox<E> {
    
    }
    Code:
    package main;
    
    public class BoxBasic<E> extends ABox<E> {
    
        private E s;
    
        public BoxBasic(E s) {
            this.s = s;
        }
    
        public E getS() {
            return s;
        }
    
        public void setS(E s) {
            this.s = s;
        }
    
        @Override
        public String toString() {
            return s.toString();
        }
    
    }
    Code:
    package main;
    
    import java.util.List;
    
    public class BoxList<E> extends ABox<E> {
    
        private List<ABox<E>> list;
    
        public BoxList(List<ABox<E>> list) {
            this.list = list;
        }
    
        public List<ABox<E>> getList() {
            return list;
        }
    
        public void setList(List<ABox<E>> list) {
            this.list = list;
        }
    
        @Override
        public String toString() {
            String res = "";
            for (ABox<E> a : list)
                res += a.toString() + ",";
            if (list.isEmpty())
                return "(" + res + ")";
            else
                return "(" + res.substring(0, res.length() - 1) + ")";
        }
    
    }
    Code:
    package main;
    
    public class BoxPair1<E> extends ABox<E> {
    
        private ABox<E> S;
        private int val;
    
        public BoxPair1(ABox<E> S, int val) {
            this.S = S;
            this.val = val;
        }
    
        public ABox<E> getS() {
            return S;
        }
    
        public int getVal() {
            return val;
        }
    
        public void setS(ABox<E> s) {
            S = s;
        }
    
        public void setVal(int val) {
            this.val = val;
        }
    
        @Override
        public String toString() {
            return "(" + S.toString() + "," + val + ")";
        }
    
    }
    Code:
    package main;
    
    public class BoxPair2<E> extends ABox<E> {
    
        private ABox<E> S1;
        private ABox<E> S2;
    
        public BoxPair2(ABox<E> S1, ABox<E> S2) {
            this.S1 = S1;
            this.S2 = S2;
        }
    
        public ABox<E> getS1() {
            return S1;
        }
    
        public ABox<E> getS2() {
            return S2;
        }
    
        public void setS1(ABox<E> s1) {
            S1 = s1;
        }
    
        public void setS2(ABox<E> s2) {
            S2 = s2;
        }
    
        @Override
        public String toString() {
            return "(" + S1.toString() + "," + S2.toString() + ")";
        }
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Standard Template Library Programmer's Guide
    Contains vectors, strings, lists, pairs and so on.

    If you're going to learn C++, then learn C++ from the beginning.

    There is a lot more to it than simply performing a few global edits on Java code, and out pops C++ code.
    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.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Furthermore, you have to show your attempt first.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Hi!

    I have tried the following which results in
    Code:
    $ gcc foo.C
    foo.C:11:17: error: ‘std::string HasToString::toString()’ is inaccessible
    foo.C:49:1: error: within this context
    foo.C: In member function ‘std::string BoxPair2::toString()’:
    foo.C:11:17: error: ‘std::string HasToString::toString()’ is inaccessible
    foo.C:61:31: error: within this context
    foo.C:11:17: error: ‘std::string HasToString::toString()’ is inaccessible
    foo.C:61:58: error: within this context
    I have tried to search for the error message but I believe that what I found has nothing to do with my problem...
    I guess that some hiding rather than the expected instantiation occurs…

    So I am interested in (a) an easy fix for my problem (b) any further suggestions on how to do it better ;-)

    Cheers!


    Code:
    #include <map>
    #include <string>
    #include <stdio.h>
    #include <boost/lexical_cast.hpp>
    
    // using namespace std;
    
    class HasToString
    {
      public:
        std::string toString();
    };
    
    class ABox : public HasToString
    {
        using HasToString::toString;
    };
    
    class BoxBasic : public ABox
    {
      public:
        BoxBasic(std::string name) : ABox()
        {
          this->name = name;
        }
        std::string name;
        std::string toString ()
        {
        return (this->name);
        };
    
    };
    
    class BoxPair1 : public BoxBasic
    {
      public:
        BoxPair1(std::string name, int annot) : BoxBasic(name)
        {
          this->annot = annot;
        }
        int annot;
        std::string toString ()
        {
        return (this->name) + boost::lexical_cast<std::string>(this->annot);
        };
    };
    
    class BoxPair2 : public ABox
    {
      public:
        BoxPair2(ABox ne1, ABox ne2) : ABox()
        {
          this->ne1 = ne1;
          this->ne2 = ne2;
        }
        ABox ne1;
        ABox ne2;
        using HasToString::toString;
        std::string toString ()
        {
        return ((this->ne1).toString()) + ((this->ne2).toString());
        };
    };
    
    int main()
    {
    
      BoxPair1 p1 ("Test1", 1);
      BoxPair1 p2 ("Test2", 2);
      BoxBasic p3 ("Test3");
      BoxPair2 p4 (p1, p2);
      BoxPair2 p5 (p3, p4);
      printf ("1 %s\n", p1.toString().c_str());
      printf ("2 %s\n", p2.toString().c_str());
      printf ("3 %s\n", p3.toString().c_str());
      printf ("4 %s\n", p4.toString().c_str());
      printf ("5 %s\n", p5.toString().c_str());
      
      return 0;
    }
    Last edited by Solour; 03-01-2013 at 11:22 AM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    foo.C:11:17: error: ‘std::string HasToString::toString()’ is inaccessible
    foo.C:49:1: error: within this context
    Do you realize that in C++ the default access method in a class is private?

    Jim

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Hi,

    I have missed a "public" somewhere?
    Would be great to know where ;-)
    Actually I thought about that already but I was unable to figure it out...

    Cheers!

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    How about here, you know somewhere around the lines mentioned in your error message (11:17)?
    Code:
    class ABox : public HasToString
    {
        using HasToString::toString;
    };
    Also your file name should probably end in something like .cpp instead of .C. Normally C++ uses the .cpp extension and a C program will use .c.

    Jim

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    I tried
    Code:
    class ABox : public HasToString
    {
      public:
        using HasToString::toString;
    };
    but it gives me same errors as when removing the body entirely.

    Code:
    $ g++ foo.cpp 
    /tmp/cci8IFXK.o: In function `BoxPair2::toString()':
    foo.cpp:(.text._ZN8BoxPair28toStringEv[BoxPair2::toString()]+0x20): undefined reference to `HasToString::toString()'
    foo.cpp:(.text._ZN8BoxPair28toStringEv[BoxPair2::toString()]+0x3a): undefined reference to `HasToString::toString()'
    collect2: ld returned 1 exit status
    this is my current version…

    Code:
    #include <map>
    #include <string>
    #include <stdio.h>
    #include <boost/lexical_cast.hpp>
    
    // using namespace std;
    
    class HasToString
    {
      public:
        std::string toString();
    };
    
    class ABox : public HasToString
    {
    };
    
    class BoxBasic : public ABox
    {
      public:
        BoxBasic(std::string name) : ABox()
        {
          this->name = name;
        }
        std::string name;
        std::string toString ()
        {
        return (this->name);
        };
    
    };
    
    class BoxPair1 : public BoxBasic
    {
      public:
        BoxPair1(std::string name, int annot) : BoxBasic(name)
        {
          this->annot = annot;
        }
        int annot;
        std::string toString ()
        {
        return (this->name) + boost::lexical_cast<std::string>(this->annot);
        };
    };
    
    class BoxPair2 : public ABox
    {
      public:
        BoxPair2(ABox ne1, ABox ne2) : ABox()
        {
          this->ne1 = ne1;
          this->ne2 = ne2;
        }
        ABox ne1;
        ABox ne2;
        std::string toString ()
        {
        return ((this->ne1).toString()) + ((this->ne2).toString());
        };
    };
    
    int main()
    {
    
      BoxPair1 p1 ("Test1", 1);
      BoxPair1 p2 ("Test2", 2);
      BoxBasic p3 ("Test3");
      BoxPair2 p4 (p1, p2);
      BoxPair2 p5 (p3, p4);
      printf ("1 %s\n", p1.toString().c_str());
      printf ("2 %s\n", p2.toString().c_str());
      printf ("3 %s\n", p3.toString().c_str());
      printf ("4 %s\n", p4.toString().c_str());
      printf ("5 %s\n", p5.toString().c_str());
      
      return 0;
    }

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Where have you implemented the HasToString::toString() function?

    Jim

  10. #10
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    I fixed it myself…
    What I would have needed was a pointer to using "virtual"…

    Code:
    #include <map>
    #include <string>
    #include <stdio.h>
    #include <boost/lexical_cast.hpp>
    
    // using namespace std;
    
    class HasToString
    {
      public:
        virtual std::string toString()=0;
    };
    
    class ABox : public HasToString
    {
      public:
        virtual std::string toString()=0;
    };
    
    class BoxBasic : public ABox
    {
      public:
        BoxBasic(std::string name) : ABox()
        {
          this->name = name;
        }
        std::string name;
        std::string toString ()
        {
        return "("+(this->name)+")";
        };
    };
    
    class BoxPair1 : public ABox
    {
      public:
        BoxPair1(ABox* ne, int annot) : ABox()
        {
          this->ne = ne;
          this->annot = annot;
        }
        ABox* ne;
        int annot;
        std::string toString ()
        {
        return "("+((*(this->ne)).toString()) +","+ boost::lexical_cast<std::string>(this->annot)+")";
        };
    };
    
    class BoxPair2 : public ABox
    {
      public:
        BoxPair2(ABox* ne1, ABox* ne2) : ABox()
        {
          this->ne1 = ne1;
          this->ne2 = ne2;
        }
        ABox* ne1;
        ABox* ne2;
        std::string toString ()
        {
        return "("+((*(this->ne1)).toString()) +","+ ((*(this->ne2)).toString())+")";
        };
    };
    
    int main()
    {
      BoxBasic b1 ("Test1");
      printf ("1 %s\n", b1.toString().c_str());
      BoxBasic b2 ("Test2");
      printf ("2 %s\n", b2.toString().c_str());
      BoxBasic b3 ("Test3");
      printf ("3 %s\n", b3.toString().c_str());
          
      BoxPair1 p1 (&b1, 1);
      printf ("1 %s\n", p1.toString().c_str());
      BoxPair1 p2 (&b2, 2);
      printf ("2 %s\n", p2.toString().c_str());  
      BoxPair2 p4 (&p1, &p2);
      printf ("3 %s\n", p4.toString().c_str());  
      
      BoxPair2 p5 (&b1, &p4);
      printf ("4 %s\n", p5.toString().c_str());
      
      return 0;
    }

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by jimblumberg View Post
    Also your file name should probably end in something like .cpp instead of .C. Normally C++ uses the .cpp extension and a C program will use .c.

    Jim
    Technically, .C, with a capital c is a standard c++ extention. However, it will not work on Windows, because the windows file-system is case agnostic. So this is good advice.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 09-23-2012, 03:06 PM
  2. Replies: 4
    Last Post: 03-22-2012, 01:11 PM
  3. stl for pairs
    By dpp in forum C++ Programming
    Replies: 14
    Last Post: 05-18-2009, 09:46 AM
  4. pairs of vector..
    By Luigi in forum C++ Programming
    Replies: 4
    Last Post: 12-20-2002, 02:09 AM
  5. abstract vs pure abstract class
    By Kohatian 3279 in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2002, 11:12 AM