473,239 Members | 1,646 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,239 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 2163
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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.