473,394 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Vector of objects and create DLL problem

21
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::student(); //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<student::student * > registeredstud::get_registered_students(); //I read online that I need the * in here. In fact this reduces my errors too
{
vector< student::student * > 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 registeredstudent
{
public:
registeredstudent::registerstudent();
std::vector< student::student* > get_registered_students();
}
}

I get the following errors:
error C3699: '*' : cannot use this indirection on type "student::student"
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
Jul 6 '07 #1
9 2169
weaknessforcats
9,208 Expert Mod 8TB
student class

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

Expand|Select|Wrap|Line Numbers
  1. class Student{
  2. {
  3.    public:
  4.        student::student(); //constructor
  5.         //this contains all the methods for getting and setting student records
  6. };
  7.  
You then create Student objects and put them in the vector:
Expand|Select|Wrap|Line Numbers
  1. Student   s;
  2. vector<Student> v;
  3. v.push_back(s);
  4.  
Jul 7 '07 #2
Jazi
21
I think this should be:

Expand|Select|Wrap|Line Numbers
  1. class Student{
  2. {
  3.    public:
  4.        student::student(); //constructor
  5.         //this contains all the methods for getting and setting student records
  6. };
  7.  
You then create Student objects and put them in the vector:
Expand|Select|Wrap|Line Numbers
  1. Student   s;
  2. vector<Student> v;
  3. v.push_back(s);
  4.  
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
Jul 9 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Then just out your vector in another class:

Expand|Select|Wrap|Line Numbers
  1. class XYZ
  2. {
  3.      private:
  4.         vector<Student> students;
  5. };
  6.  
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:

Expand|Select|Wrap|Line Numbers
  1. vector<Student>::iterator   XYZ::GetFirstStudent()
  2. {
  3.       return this->students.begin();
  4. }
  5.  
Try to keep only one vector in existence.
Jul 9 '07 #4
Jazi
21
Then just out your vector in another class:

Expand|Select|Wrap|Line Numbers
  1. class XYZ
  2. {
  3.      private:
  4.         vector<Student> students;
  5. };
  6.  
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:

Expand|Select|Wrap|Line Numbers
  1. vector<Student>::iterator   XYZ::GetFirstStudent()
  2. {
  3.       return this->students.begin();
  4. }
  5.  
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::string nm){
name=nm;
}
std::string get_name(void){
return name;
}
};

class classB{
public:
void get_classAObjects(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_back(atemp);
}
}

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

}
//private:
std::vector<classA> students;
};


int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
classB cb;
cb.get_classAObjects(5);

//std::vector<classB> nv;
cout<<"Size2:"<<cb.students.size();
//classA ca;
classA ca=cb.GetFirstStudent();
cout<<"First :"<<ca.get_name<<endl;

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


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

Is there a better way to access the objects than the above way? I appreciate your help
Jul 9 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
The error happens at
classA ca=cb.GetFirstStudent();
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:

Expand|Select|Wrap|Line Numbers
  1. int main(array<System::String ^> ^args)
  2. {
  3. Console::WriteLine(L"Hello World");
  4.  
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#.
Jul 10 '07 #6
Jazi
21
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:

Expand|Select|Wrap|Line Numbers
  1. int main(array<System::String ^> ^args)
  2. {
  3. Console::WriteLine(L"Hello World");
  4.  
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
Jul 10 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. class MyClass
  2. {
  3.       public:
  4.         MyClass(int);
  5. };
  6.  
When you create a MyClass object:
Expand|Select|Wrap|Line Numbers
  1. MyClass obj = 10;
  2.  
the compiler will call MyClass::MyClass(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.GetFirstStudent();
[/code]

needs to call ClassA::ClassA(vector<classA>::iterator arg), which is not in ClassA.
Jul 10 '07 #8
Jazi
21
[quote=weaknessforcats]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.

Expand|Select|Wrap|Line Numbers
  1. class MyClass
  2. {
  3.       public:
  4.         MyClass(int);
  5. };
  6.  
When you create a MyClass object:
Expand|Select|Wrap|Line Numbers
  1. MyClass obj = 10;
  2.  
the compiler will call MyClass::MyClass(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:
classA ca=cb.GetFirstStudent();
[/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.GetFirstStudent();
Jul 10 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
I just needed to dereference the iterator
classA ca=*cb.GetFirstStudent();
Too simple! I should have noticed that.
Jul 11 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tran Tuan Anh | last post by:
Dear all, I am new in C++, and now get confused about a lot of things. I wrote this simple code to test the vector. class Temp { public: int x; }; int main() { vector<Temp> v;
9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
2
by: Clement RAMBACH | last post by:
Hi, here is my problem: I have a std::vector< A* >. This vector contains pointers to the objects A i create (lets say about 4000 items). I do this several times in my application, and at the...
10
by: gogogo_1001 | last post by:
Dear all, I don't understand why "delete" works well on destructing a object, but fails to destruct a vector of it. Any of your comment is highly appreciated! Following is the program...
11
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
3
by: nw | last post by:
Hi, I have three classes, a template pure virtual base class, a template derived class and a third which I would like to use to store copies of the derived class. The code looks like this: ...
13
by: ragged_hippy | last post by:
Hi, I wanted to create a vector of const references. Something like this: vector<const x&y; where x is a class name. Is this a valid statement? Will this work if I use a reference of 'y'...
10
by: Jess | last post by:
Hello, I have a program that stores dynamically created objects into a vector. #include<iostream> #include<vector> using namespace std;
3
by: Rob | last post by:
I have these classes (elided methods): class Base { public: Base(string name) {...} }; class Derived : public Base {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.