Vector of objects and create DLL problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jazi
    New Member
    • Jun 2007
    • 21

    Vector of objects and create DLL problem

    Hello everyone,

    I was wondering if someone can help me figuring out some errors I keep getting when I try to create a dll library from existing c++ code using VS2005. The issue is related to a class that returns a vector of objects. There is no errors when I compile this in c++ "linux" and it works find but the issue is with the VS2005. Here is what I have:

    I have two classes:

    student class

    namespace StudentClass{
    student::studen t(); //constructor
    //this contains all the methods for getting and setting student records
    }

    registered students class //contains method that returns a class of student's objects based on some kind of criteria

    namespace RegStudentClass {
    registeredstud: :registeredstud (); //constructor

    std::vector<stu dent::student * > registeredstud: :get_registered _students(); //I read online that I need the * in here. In fact this reduces my errors too
    {
    vector< student::studen t * > myStudVect; /temp vector
    //logic to get the list of my students' objects and push_pack to the myStudVect vector
    return myStudVect;
    }
    }

    in the header file for this class

    namespace RegStudentClass {
    public ref class registeredstude nt
    {
    public:
    registeredstude nt::registerstu dent();
    std::vector< student::studen t* > get_registered_ students();
    }
    }

    I get the following errors:
    error C3699: '*' : cannot use this indirection on type "student::stude nt"
    Basically this error appears whereever I called the vector<student: :student *>.

    Can someone tell me what is the cause of this error and how to resolve it. Also if there is a better way to create the vector than the way I am doing it. I would appreciate any kind of help.

    Jazi
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Originally posted by jazi
    student class

    namespace StudentClass{
    student::studen t(); //constructor
    //this contains all the methods for getting and setting student records
    }
    I think this should be:

    [code=cpp]
    class Student{
    {
    public:
    student::studen t(); //constructor
    //this contains all the methods for getting and setting student records
    };
    [/code]

    You then create Student objects and put them in the vector:
    [code=cpp]
    Student s;
    vector<Student> v;
    v.push_back(s);
    [/code]

    Comment

    • Jazi
      New Member
      • Jun 2007
      • 21

      #3
      Originally posted by weaknessforcats
      I think this should be:

      [code=cpp]
      class Student{
      {
      public:
      student::studen t(); //constructor
      //this contains all the methods for getting and setting student records
      };
      [/code]

      You then create Student objects and put them in the vector:
      [code=cpp]
      Student s;
      vector<Student> v;
      v.push_back(s);
      [/code]
      This part I understand and that is what I am doing, however; I would like the vector to be in a method within another class. So whenever I need this vector I just call this method and I should be able to get the vector of students' objects.

      The reason why I want this vector to be in a separate class is that I have other properties that I want to add to this class as well as other methods too. I did not include this in my first post because I do not want to confuse people.

      I hope this is clear. Also if there is a link that I can get more examples on how to use vector of objects and how to call it, that will be great. I appreciate your help

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Then just out your vector in another class:

        [code=cpp]
        class XYZ
        {
        private:
        vector<Student> students;
        };
        [/code]

        However, do not write member functions on XYZ that return vectors. When you do that copies of the vector are made. Instead, write methods that return an iterator to a Student in the vector:

        [code=cpp]
        vector<Student> ::iterator XYZ::GetFirstSt udent()
        {
        return this->students.begin ();
        }
        [/code]

        Try to keep only one vector in existence.

        Comment

        • Jazi
          New Member
          • Jun 2007
          • 21

          #5
          Originally posted by weaknessforcats
          Then just out your vector in another class:

          [code=cpp]
          class XYZ
          {
          private:
          vector<Student> students;
          };
          [/code]

          However, do not write member functions on XYZ that return vectors. When you do that copies of the vector are made. Instead, write methods that return an iterator to a Student in the vector:

          [code=cpp]
          vector<Student> ::iterator XYZ::GetFirstSt udent()
          {
          return this->students.begin ();
          }
          [/code]

          Try to keep only one vector in existence.
          Thank you again for your help. I see what you are doing. It looks like much better than what I am trying to achieve.

          Here is what I did (I am using VS2005). This should make a different. I keep getting the following error

          #include "stdafx.h"
          #include <iostream>
          #include <vector>
          #include <string>

          using namespace System;
          using namespace std;

          class classA
          {
          std::string name;
          public:
          void set_name(std::s tring nm){
          name=nm;
          }
          std::string get_name(void){
          return name;
          }
          };

          class classB{
          public:
          void get_classAObjec ts(int nom){
          //cout<<"we are here"<<endl;
          for(int i=0; i<nom; i++)
          {
          cout<<"I:"<<i<< endl;
          classA atemp;
          atemp.set_name( "name 1");
          students.push_b ack(atemp);
          }
          }

          vector<classA>: :iterator classB::GetFirs tStudent()
          {
          return this->students.begin ();

          }
          //private:
          std::vector<cla ssA> students;
          };


          int main(array<Syst em::String ^> ^args)
          {
          Console::WriteL ine(L"Hello World");
          classB cb;
          cb.get_classAOb jects(5);

          //std::vector<cla ssB> nv;
          cout<<"Size2:"< <cb.students.si ze();
          //classA ca;
          classA ca=cb.GetFirstS tudent();
          cout<<"First :"<<ca.get_name <<endl;

          //for(std::vector <classA>::itera tor p=cb.students.b egin(); p!=cb.students. end();p++){
          // cout<<"new:"<<p <<endl;
          // }
          return 0;
          }


          The error happens at
          classA ca=cb.GetFirstS tudent();
          cout<<"First :"<<ca.get_name <<endl;

          Is there a better way to access the objects than the above way? I appreciate your help

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by Jazi
            The error happens at
            classA ca=cb.GetFirstS tudent();
            I would like to say that's becuse there isn't a constructor for ClassB that takes a vector<Student> ::iteratror argument. However, I also see this:

            [code=cpp]
            int main(array<Syst em::String ^> ^args)
            {
            Console::WriteL ine(L"Hello World");
            [/code]

            That is C# code. This is trhe C++ forum. Do I need to move this thread to the .NET forum?? I am giving you C++ answers that may not apply in C#.

            Comment

            • Jazi
              New Member
              • Jun 2007
              • 21

              #7
              Originally posted by weaknessforcats
              I would like to say that's becuse there isn't a constructor for ClassB that takes a vector<Student> ::iteratror argument. However, I also see this:

              [code=cpp]
              int main(array<Syst em::String ^> ^args)
              {
              Console::WriteL ine(L"Hello World");
              [/code]

              That is C# code. This is trhe C++ forum. Do I need to move this thread to the .NET forum?? I am giving you C++ answers that may not apply in C#.
              It is my mistake. I was working with VS2005 and that what I sent you. However, consider this a C++ problem and remove those two lines if you do not mind from the code.
              In regard to the main error, I guess I am not sure what you mean by " that's becuse there isn't a constructor for ClassB that takes a vector<Student> ::iteratror argument". Can you please tell me how would you go about it? I am really new to all of these stuff and I am trying to learn by examples. I appreciate your help
              jazi

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                A constructor is a class member function that the compiler will call automatically when you create an object. Constructors are identified as member functions whose name is the same as the class name.

                [code=cpp]
                class MyClass
                {
                public:
                MyClass(int);
                };
                [/code]

                When you create a MyClass object:
                [code=cpp]
                MyClass obj = 10;
                [/code]

                the compiler will call MyClass::MyClas s(int) which will properly initialize the object. What you see above is not an assignment. If this constructor does not exist, the program won't compile.

                In your case this code to create the object ca:
                [quote=Jaazi]
                classA ca=cb.GetFirstS tudent();
                [/code]

                needs to call ClassA::ClassA( vector<classA>: :iterator arg), which is not in ClassA.

                Comment

                • Jazi
                  New Member
                  • Jun 2007
                  • 21

                  #9
                  [QUOTE=weaknessf orcats]A constructor is a class member function that the compiler will call automatically when you create an object. Constructors are identified as member functions whose name is the same as the class name.

                  [code=cpp]
                  class MyClass
                  {
                  public:
                  MyClass(int);
                  };
                  [/code]

                  When you create a MyClass object:
                  [code=cpp]
                  MyClass obj = 10;
                  [/code]

                  the compiler will call MyClass::MyClas s(int) which will properly initialize the object. What you see above is not an assignment. If this constructor does not exist, the program won't compile.

                  In your case this code to create the object ca:
                  Originally posted by Jaazi
                  classA ca=cb.GetFirstS tudent();
                  [/code]

                  needs to call ClassA::ClassA( vector<classA>: :iterator arg), which is not in ClassA.
                  Thanks for your help.

                  I just needed to dereference the iterator
                  classA ca=*cb.GetFirst Student();

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Originally posted by Jazi
                    I just needed to dereference the iterator
                    classA ca=*cb.GetFirst Student();
                    Too simple! I should have noticed that.

                    Comment

                    Working...