接收错误C4700,无法完成驱动程序
Receiving error C4700 and unable to finish driver program
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <cctype>
using namespace std;
struct Student {
char* name;
float gpa;
};
Student * createStudent( char name[], float gpa) {
Student * studentPtr;
studentPtr = new Student;
studentPtr->name = new char[strlen(name) + 1];
studentPtr->name = name;
studentPtr->gpa = gpa;
// Return the pointer
return studentPtr;
}
bool destroyStudent(Student*& aStudent) {
if (aStudent) { //check whether this pointer is already null.
free(aStudent->name);
delete aStudent;
aStudent = nullptr;
return true;
}
return false; //aStudent is already null
}
int main() {
Student * student1 = createStudent(student1->name, student1->gpa); //error is here
cout << "Enter Student's name: " << endl;
cin >> student1->name;
cout << "Enter GPA: " << endl;
cin >> student1->gpa;
cout << student1->name << " and " << student1->gpa << endl;
destroyStudent(student1);
if (student1) {
cout << "Pointer is NOT null!!!" << endl;
student1 = nullptr;
}
if (!student1) {
cout << "The pointer is null now." << endl;
}
return 0;
}
我正试图完成一个驱动程序,在那里我可以测试我的功能,但我一直没有成功。对于我所做的每一个更改,它只会添加更多的错误消息。这是我能得到的最接近的错误,只有一个我无法解决的错误。
错误信息:
错误1:使用未初始化的本地变量"student1"
非常感谢任何帮助。我已经在这里搜索了这个错误,无法解决自己:(
你到底想干什么:
Student * student1 = createStudent(student1->name, student1->gpa);
这是完全错误的!你试图用你试图创建的对象的字段创建一个Student
对象——你没有看到这里的逻辑错误吗?
正确的用法之一是:
Student * student1 = createStudent("Smith", 4.0f);
不能使用尚未创建的对象,这样做会导致错误。
相关文章:
- Mongdb C++ 驱动程序编译错误 for document{}.
- 如何找到 NVIDIA OpenGL 驱动程序的错误 ID 名称?
- 编译MongoDB C 11驱动程序的错误
- Apache IGNITE编译ODBC驱动程序错误
- Apache Ignite v2.7:无法构建 Linux ODBC 驱动程序 - 内联变量编译错误
- Microsoft访问 ODBC 驱动程序管理器函数序列错误
- QT 5.8 SQL连接错误:Windows 10上未加载QMYSQL驱动程序
- 错误 = 0x801F0020筛选器回复消息'error_flt_no_waiter_for_reply'微筛选器驱动程序
- 如何使用 C++ 驱动程序在 MongoDB 中使用 UserException 处理数据类型错误
- 从 UMDF 驱动程序调用创建文件时出现'Access is denied'错误 (C++)
- 在Ubuntu中包括MongoDB C 驱动程序的错误
- Cassandra cpp 驱动程序抛出错误,但能够与集群连接
- MongoDB C++ 驱动程序 -- 通过引用传递查询对象在随后使用其排序时会引发错误
- 错误 C2220:警告被视为驱动程序开发中生成的错误无'object'文件
- Mongo新的c++驱动程序错误
- GPU 驱动程序错误处理
- 使用 C++ 旧驱动程序的 mongodb 查询会导致 BSONElement:错误类型 -64
- Sybase DB 错误:数据库驱动程序错误.发送失败
- MongoDB C++ 驱动程序 - 错误:尚未声明"mongo::client"
- 简单的CUDA应用程序,cudaMalloc以错误结束:未指定的驱动程序错误