0

I use the following function to generate a dynamic 2d array in c:

int** Make2DintArray(int arraySizeX, int arraySizeY) { // From http://pleasemakeanote.blogspot.com/2008/06/2d-arrays-in-c-using-malloc.html
    int** theArray;
    theArray = (int**) malloc(arraySizeX*sizeof(int*));
    for (int i = 0; i < arraySizeX; i++)
        theArray[i] = (int*) malloc(arraySizeY*sizeof(int));
    return theArray;
} 

I wanted to concatenate all the values of the Matrix (2d - array) to form a string separated by #

I used

int hi11cipherSize = 20;
std::ostringstream text2Encrypt;///ERROR SOURCE
// i used the above function to creat a 2D array and fill it 
// then i used the code below to loop through the record and create the string with delimiter #

for(int i =0; i<hi11cipherSize;i++){
    for(int j =0; j<hi11cipherSize;j++){
        printf("%d \t",HillCipherMatrix[i][j]);
        temp = HillCipherMatrix[i][j];
        text2Encrypt<<(char)temp<<"#";  //// First error cannot concatenate int with char ???

    }   printf("\n");
}

QString tempHLBP = QString::fromStdString(text2Encrypt.str()) ;

qDebug()<<"Text to encrypt "<<tempHLBP<<"\n";
qDebug()<<"Length of string "<<tempHLBP.length();

I get the following ERRORS ERROR :

22:37:59: Running build steps for project StringManJVN...
22:37:59: Configuration unchanged, skipping qmake step.
22:37:59: Starting: "C:\QtSDK\QtCreator\bin\jom.exe" 
main.cpp
main.cpp(17) : error C2079: 'text2Encrypt' uses undefined class 'std::basic_ostringstream<_Elem,_Traits,_Alloc>'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Alloc=std::allocator<char>
        ]
main.cpp(27) : error C2297: '<<' : illegal, right operand has type 'const char [2]'
main.cpp(27) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
main.cpp(32) : error C2228: left of '.str' must have class/struct/union
        type is 'int'
jom: C:\QTProject\StringManJVN\Makefile.Debug [debug\main.obj] Error 2
    cl -c -nologo -Zm200 -Zc:wchar_t- -Zi -MDd -GR -EHsc -W3 -w34100 -w34189 -DUNICODE -DWIN32 -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_SQL_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -I"..\..\QtSDK\Desktop\Qt\4.8.1\msvc2010\include\QtCore" -I"..\..\QtSDK\Desktop\Qt\4.8.1\msvc2010\include\QtSql" -I"..\..\QtSDK\Desktop\Qt\4.8.1\msvc2010\include" -I"..\..\QtSDK\Desktop\Qt\4.8.1\msvc2010\include\ActiveQt" -I"debug" -I"..\..\QtSDK\Desktop\Qt\4.8.1\msvc2010\mkspecs\win32-msvc2010" -Fodebug\ @C:\Users\SONUT\AppData\Local\Temp\main.obj.3948.0.jom
    C:\QtSDK\QtCreator\bin\jom.exe -f Makefile.Debug

jom 1.0.8 - empower your cores

jom: C:\QTProject\StringManJVN\Makefile [debug] Error 2
22:38:02: The process "C:\QtSDK\QtCreator\bin\jom.exe" exited with code 2.
Error while building project StringManJVN (target: Desktop)
When executing build step 'Make'

Thanks in advance ;-))

6
  • 1
    did you include the stream header? Commented Jun 3, 2013 at 18:55
  • @Floris I doubt that is a correct analysis as text2encrypt is an ostream, which should allow any type usage for which the stream operators are defined. I could be wrong though Commented Jun 3, 2013 at 19:10
  • It doesn't know what text2Encrypt is based on. As mentioned by @AK4749, you may be missing an include Commented Jun 3, 2013 at 19:20
  • Even if you resolve your stream inclusion errors, it should be noted that your function returns a local pointer, which is always a bad idea. Commented Jun 3, 2013 at 19:54
  • @valekhalfheart: since the pointer is malloc'ed inside the routine, and that block remains valid, returning the value of the pointer should not be a problem. Commented Jun 4, 2013 at 0:57

1 Answer 1

1

Try to put this at the top:

#include <sstream>

Reading your above question you say that you are trying to put this into a string. If you mean an std::string, doing this is easy. Be sure to include the right string library

#include <string>

and then you can do something like:

int x = 4;
string s = "";
s += x + 48; //this will convert the number 4 into the char value for 4.
             //it works for all ascii numbers
s += "#";    //puts the # sign in there

That more or less will get you what you want. I am not sure if it is the best way, but it sure is easy. Obviously, the steps above are spead out for clarity. You can certainly condense that a bit.

Sign up to request clarification or add additional context in comments.

3 Comments

Does anyone know a way to concatenate integer with a delimiter "#" Another function than std::ostringstream <<
thanks this solved the problem by simply including #include <sstream> the problem was solved
Cool, feel free to check it as the answer if it worked for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.