By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

Problem using std::vector

P: 1
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;
std::string strData = "Two";

This compiles, links, and executes just fine.

If I do this with my object:
class CInstrument : public CObject
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?


Jun 23 '06 #1
Share this Question
Share on Google+
2 Replies

P: 16
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

Expert Mod 5K+
P: 8,916
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. //
  4. #include <afx.h>
  6. #include <vector>
  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;
  18.         return *this;
  19.     };
  20.     virtual ~CInstrument(){};
  21.     int m_dGPIBAddress;
  22. };
  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
  33.     return 0;
  34. }
Jun 25 '06 #3

Post your reply

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