strcpy 对指针函数的引用
strcpy a reference to a pointer function
我在将我的字符串从用户输入存储到文件名时遇到问题。我需要将文件名保存到 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
}
我对您的源代码进行了一些小的更改,以便对其进行测试和修复。我在 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;
}
相关文章:
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- 无法将右值引用函数与 GCC 匹配
- C ++引用函数参数似乎包含原始对象的副本,而不是充当"real reference"
- 通过引用函数传递指针参数是什么意思?
- 常量引用函数参数的地址何时唯一?
- 我想了解为什么在这个例子中使用引用函数?或者引用在c++函数中的重要性
- C++ 通过引用函数传递数组,但内容保持不变
- 用数组或指针引用函数?
- 引用函数如何"int &foo();"工作?
- 常量引用函数参数:是否可以禁止临时对象?
- 计算在代码中引用函数的次数
- <initializer_list> 引用函数
- 字符串引用(函数)的差异
- 在类中引用函数时的"No Matching Function for Call"
- 无法通过引用函数"calcValues"中"firstNum"和"secondNum"变量来更改值
- 尝试引用已删除的函数(不引用函数)
- 引用函数指针
- 引用函数按值和自动返回
- 为什么我们可以取消引用函数指针
- 通过引用函数传递值的目的是什么