473,378 Members | 1,139 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,378 software developers and data experts.

Problem using std::vector

Hi,
I am using Visual C++ 6.0 MFC. I have been trying to build an array of objects. First I tried using CArray but it was just giving me fits. Once I got my copy contructor built so I would not get compile errors I got LINK errors because of the Object.Add() function. Since so many people on the internet suggested to others to use std::vector instead, I decided to try std::vector.

If I do this:
std::string strData = "One";
std::vector<std::string> strVector;
strVector.push_back(strData)
std::string strData = "Two";
strVector.push_back(strData)

This compiles, links, and executes just fine.

If I do this with my object:
class CInstrument : public CObject
{
public:
CInstrument();
CInstrument(const CInstrument &Instrument);
CInstrument& operator = (const CInstrument);
virtual ~CInstrument();
int m_dGPIBAddress;
}

And then implement it in the .cpp file:
CInstrument Instrument;
std::vector<CInstrument> Instruments;
Instrument.m_dGPIBAddress = 9;
Instruments.push_back(Instrument); // If I comment out this line I do not get LINK errors

Does anyone see whats wrong here?

Thanks,

Buck
Jun 23 '06 #1
2 3820
I think I had a similar problem myself. I think it has trouble with the object size and structure. Try using a pointer to the object
vector<CInstrument *> obj;

and see if that works. Im not saying this is how you should use the vector array, it just might help you to figure out the problem if it does work.
Jun 23 '06 #2
Banfa
9,065 Expert Mod 8TB
OK I have used your code as the basis for the file below.

It compiles and links for me without error.

What error did you get?

Expand|Select|Wrap|Line Numbers
  1. // Tester.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <afx.h>
  5.  
  6. #include <vector>
  7.  
  8. class CInstrument : public CObject
  9. {
  10. public:
  11.     CInstrument(){};
  12.     CInstrument(const CInstrument &Instrument){
  13.         m_dGPIBAddress = Instrument.m_dGPIBAddress;
  14.     };
  15.     CInstrument& operator = (const CInstrument Instrument){
  16.         m_dGPIBAddress = Instrument.m_dGPIBAddress;
  17.  
  18.         return *this;
  19.     };
  20.     virtual ~CInstrument(){};
  21.     int m_dGPIBAddress;
  22. };
  23.  
  24.  
  25.  
  26. int main(int argc, char* argv[])
  27. {
  28.     CInstrument Instrument;
  29.     std::vector<CInstrument> Instruments;
  30.     Instrument.m_dGPIBAddress = 9;
  31.     Instruments.push_back(Instrument); // If I comment out this line I do not get LINK errors
  32.  
  33.     return 0;
  34. }
  35.  
Jun 25 '06 #3

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

Similar topics

8
by: Christian Stigen Larsen | last post by:
Consider the following: class parent { public: virtual void print() { printf("Parent\n"); } }; class child : public parent {
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
2
by: Eric | last post by:
I am converting my VC++ MFC to Manage C++ and I am having trouble using the vector class. Here is my __gc class ....other headers for System classes .... #include <algorithm> #include...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
8
by: Jason Heyes | last post by:
Does the STL have a function like this one? template <typename T> void remove(std::vector<T> &v, std::vector<T>::size_type index) { std::swap(v, v.back()); v.resize(index); } Unlike...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
6
by: nabeel.girgis | last post by:
I'm creating a deck class which uses a card class. The card class is correct and works perfectly. One fo my member functions is to draw a card, but I'm having trouble removing the top card after...
3
by: vrsathyan | last post by:
Hi.., While executing the following code in purifier.., std::vector<int> vecX; vecX.clear(); int iCount = 0; { int iVal;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.