我无法将用户输入的字符串存储到fileName中。我需要将fileName保存到GetfileName()中。
以下是我的代码片段:
class Frame {
char* fileName;
Frame* pNext;
public:
Frame();
~Frame();
char*& GetfileName() { return fileName; }
Frame*& GetpNext() { return pNext; };
};
void Animation::InsertFrame() {
Frame* frame = new Frame; //used to hold the frames
char* firstName = new char[40];
cout << "Please enter the Frame filename :";
cin.getline(firstName, 40); //enter a filename
strcpy(&frame->GetfileName, firstName); //error, need to copy the inputed name into the function getFileName that returns a char* filename
}
答案 0 :(得分:1)
我在源代码中做了一些小改动,以便测试并修复它。我在Frame类中创建了一个名为SetfileName的方法,并将char *fileName
更改为char fileName[40]
,以便Frame class
保存fileName的值而不是指针。
#include <iostream>
#include <string.h>
using namespace std;
class Frame {
char fileName[40];
Frame *pNext;
public:
Frame() {}
~Frame() {}
const char *GetfileName () { return fileName; }
const Frame *GetpNext () { return pNext; };
void SetfileName(const char *name) { strncpy(fileName, name, sizeof(fileName)); }
void printFileName() { cout << fileName << endl; }
};
void InsertFrame() {
Frame* frame = new Frame; //used to hold the frames
char* firstName = new char[40];
cout << "Please enter the Frame filename :";
cin.getline(firstName, 40); //enter a filename
frame->SetfileName(firstName);
frame->printFileName();
}
int main() {
InsertFrame();
return 0;
}