سؤال

Currently in the process of learning c++ and maybe it's because I am really frustrated right now but I really can't seem to wrap my simple little head around this:

There's a class constructor:

Class (const char* file); 

I use it like this in my main class:

char* chararr = new char[2048]; 
//stuff with charrarr
// std::cout << chararr would be: "C:\stuff\paths\etc\"
Class c( strcat(chararr , "filename.file"));
//I want it to be: "C:\stuff\paths\etc\filename.file
Class c2( strcat(chararr , "filename2.file2"));
////I want this to be: "C:\stuff\paths\etc\filename2.file2" but it is instead
// "C:\stuff\paths\etc\filename.filefilename2.file"

The problem is that strcat modifies the chararr so the second time I do this, with Class c2, it gets all messed up... I'm guessing it's a very very basic thing to do and it gets me even more frustrated knowing I'm missing something really obvious...

هل كانت مفيدة؟

المحلول

Error in you code first time you should call strcpy(), whereas you are concatenates with garbage.

Class c( strcpy(chararr , "filename.file"));

else it concatenate with garbage, undefined behavior.

Edit:

// std::cout << chararr would be: "C:\stuff\paths\etc\"
size_t len = strlen(chararr);  // <--- notice
Class c( strcat(chararr , "filename.file"));
// path\path2\filename.file
//            ^ replace `f` with '\0'          
chararr[len] = '\0'; // <--- notice
Class c2( strcat(chararr , "filename2.file2"));

نصائح أخرى

Why are you using strcat when you don't need (as it seems) to concatenate strings? You could simply do:

class Class {
public:
    Class(const char* file) {}
    // ...
};

int main() {
    Class c("filename.file");
    Class c2("filename2.file2");
}

or, if you really need to concatenate strings use std::string instead of const char*.

std::string is easier to manipulate, grows in size automatically and deletes itself automatically. The only gotcha in this case, is that the class must make its own copy of the text you pass as the pointer, because when the string changes data or goes out of scope, the pointer is no longer valid.

std::string mystr = "filename.file";
MyClass c(mystr.c_str());
mystr = "filename2"; // reset string
mystr += ".file2" // concatenate
MyClass c2(mystr.c_str());

If you wrote the class, change it to use std::string as well.

class MyClass
{
public:
    MyClass(const std::string& str_in)
        : str(str_in) // initialization list
    {
    }

    std::string str;
};

std::string mystr = "filename.file";
MyClass c(mystr);
mystr = "filename2"; // reset string
mystr += ".file2" // concatenate
MyClass c2(mystr);

You have to reinitialize chararr if you want to use it this way:

char* chararr = new char[2048]; 

strcpy(chararr,"path\\path2\\");
Class c( strcat(chararr , "filename.file"));
strcpy(chararr,"path\\path2\\");
Class c2( strcat(chararr , "filename2.file2"));

For that you're using c++ I'd recommend using std::string anyway.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top