3

I'm working with some legacy code and I need a StoreStrings class storing some strings and able to return a MyString*.

I've tried with this:

typedef char MyString[64]; 

class StoreStrings{
public: 
    void store(MyString *aStr)
    {
        theVec.push_back(aStr);
    }
    const MyString* get(){return theVec.begin();} 
private: 
    std::vector<MyString> theVec;
};

But I'm disappointed since it doesn't compile with this syntax.

StoreStrings myStore;

myStore.store("Hello");//cannot convert parameter 1 from 'char [6]' to 'char (*)[64]'

I've to instantiate one MyString before.

MyString myStr = "Hello";
myStore.store(&myStr);

How can I rewrite the StoreStrings class so to have myStore.store("Hello"); compiling?

jimifiki
  • 5,377
  • 2
  • 34
  • 60
  • @RahulTripathi That's C# isn't it? – Bernhard Barker Aug 12 '13 at 13:55
  • @rahul I've tried few possible solutions, even using string.c_str() Can you come out with a piece of c++ code? – jimifiki Aug 12 '13 at 13:56
  • char mystring[64] is *not* a declaration of a variable length C string. And "hello" does not have 64 characters. Use const char* or std::string instead. – Hans Passant Aug 12 '13 at 14:16
  • MyString is part of the legacy and is not intended to be variable size. 64 is just the largest size a string is assumed to have. – jimifiki Aug 12 '13 at 14:21
  • void store(MyString *aStr), MyString is already a char*,, also u define theVec as vector whereas u are trying to insert MyString* – aamir Aug 12 '13 at 15:41

2 Answers2

1

I would suggest something like that

Storestring.h

#pragma once
#include <vector>
class StoreStrings
{
public: 
void store(const char* aStr)
{
  pszStr = new char[64];
  strcpy_s(pszStr,64,aStr);
    theVec.push_back(pszStr);
};
~StoreStrings(void){
for(std::vector<char*>::iterator it = theVec.begin();it!=theVec.end();++it){
 delete *it;  
}
};

std::vector<char*>::iterator getBegin(){return theVec.begin();};
std::vector<char*>::iterator getEnd(){return theVec.end();};
private: 
char* pszStr;
std::vector<char*> theVec;
};

main.cpp

#include "StoreStrings.h"
#include <iostream>
int main(void){
StoreStrings s;
s.store("a");
s.store("b");
s.store("c");
for(std::vector<char*>::iterator it = s.getBegin();it!=s.getEnd();++it){
    std::cout << *it<<std::endl;
}
return 0;
};
okaerin
  • 789
  • 5
  • 23
  • Me too I was trying something of the kind. I still get "cannot convert parameter 1 from 'char *' to 'const char (&)[64]'" does it compile with your compiler? – jimifiki Aug 12 '13 at 14:19
  • I didn't really checked my suggestion. now I edited it and it does compile and work – okaerin Aug 13 '13 at 07:43
1

Arrays cannot be used in STL containers as it requires the type to be copy constructible and assignable

You may try following, however std::string approach is best.

typedef char MyString[64];

struct X{
 MyString s;
};

class StoreStrings{
public: 
    void store(MyString aStr)
    {  
        X temp ; 
        for(int i=0;aStr[i];++i)
          temp.s[i] =*(aStr+i);
        theVec.push_back(temp); 
    }
   // Here iterator is returned.
   const std::vector<X>::iterator get(){return theVec.begin();} 
private: 
    std::vector<X> theVec;
};

int main(){
    StoreStrings myStore;
    MyString m ="Hello";
    myStore.store(m);
}
P0W
  • 46,614
  • 9
  • 72
  • 119
  • Is this line going to help me? const MyString* get2(){return static_cast ((void*) &*theVec.begin());} i.e. can I assume that X has the same size than MyString? – jimifiki Aug 12 '13 at 14:38
  • I suggest to edit the for loop with strncpy(myTemp1.s,aFirst,sizeof(FMRtyLongString)); Is that meaningfull? – jimifiki Aug 12 '13 at 14:51
  • why is struct X{ MyString s;}; copy constructible and assignable? And why MyString is not? – jimifiki Aug 13 '13 at 04:08
  • 1
    @jimifiki With array following won't work:`int x[5]; int y[5]; x=y;` – P0W Aug 13 '13 at 05:09
  • Thanks! I've just read here http://stackoverflow.com/questions/2241699/is-shallow-copy-sufficient-for-structures-with-char that in general, given struct X d1, d2; d2 = d1; is equivalent to memcpy(&d2, &d1, sizeof d2). This complete the answer to my question ;-) – jimifiki Aug 13 '13 at 06:16