C++错误:不能将指针转换为引用(NETBESE)
好的,我不确定我的名字是否正确,但问题是。我一直试图将包含用户定义的课程对象的数组传递给函数,但它给我的错误是无法转换“课程*&courseCatalog”。 另外,如果我的代码有点难读,我也很抱歉。很多都被注释掉了,所以我可以测试一些东西 我的主要意见是:C++错误:不能将指针转换为引用(NETBESE),c++,arrays,pointers,netbeans,user-defined-types,C++,Arrays,Pointers,Netbeans,User Defined Types,好的,我不确定我的名字是否正确,但问题是。我一直试图将包含用户定义的课程对象的数组传递给函数,但它给我的错误是无法转换“课程*&courseCatalog”。 另外,如果我的代码有点难读,我也很抱歉。很多都被注释掉了,所以我可以测试一些东西 我的主要意见是: #include "cc.h" int main(int argc, char** argv) { bool again=1; Course c; ifstream catalogIn("catalog.dat")
#include "cc.h"
int main(int argc, char** argv) {
bool again=1;
Course c;
ifstream catalogIn("catalog.dat");
ofstream catalogOut("catalog.dat");
Course courseCatalog[256];
loadInCourses(catalogIn, courseCatalog);
courseCatalog[getNumCourses(courseCatalog)+1]=addCourse();
loadOutCourses(catalogOut, courseCatalog);
return 0;
}
我的职能:
#include "cc.h"
int displayMainMenu(){
int choice;
cout<<endl<<"**********MAIN MENU**********"<<endl;
cout<<"1. Print List Schedule"<<endl;
cout<<"2. Print Weekly Schedule"<<endl;
cout<<"3. Get Course"<<endl;
cout<<"4. Advanced Options"<<endl;
cout<<"0 to Quit"<<endl;
cout<<"*****************************"<<endl<<endl;
cout<<"Menu Choice: ";
cin>>choice;
return choice;
}
int displayAdvMenu(){
int choice;
cout<<endl<<"**********ADV MENU**********"<<endl;
cout<<"1. Add Course to Catalog"<<endl;
cout<<"2. Remove Duplicates"<<endl;
cout<<"0 to go Back"<<endl;
cout<<"****************************"<<endl<<endl;
cout<<"Menu Choice: ";
cin>>choice;
return choice;
}
Course addCourse(){
Course course;
cout<<"\nCourse name:\t\t";
cin.ignore();
cin.getline(course.name, CNAMESIZE);
cout<<"Course ID:\t\t";
cin.getline(course.id, CIDSIZE);
cout<<"Number of credits:\t";
cin>>course.credits;
return course;
}
void loadInCourses(ifstream & ifs, Course courseCatalog[]){
int i=0;
while(ifs>>courseCatalog[i].credits){
ifs.getline(courseCatalog[i].name, CNAMESIZE, ',');
ifs.getline(courseCatalog[i].id, CIDSIZE, ',');
ifs.getline(courseCatalog[i].professor, CPROFSIZE, ',');
ifs.getline(courseCatalog[i].semester, CSEMSIZE, ',');
ifs.get(courseCatalog[i].grade);
i++;
}
return;
}
void loadOutCourses(ofstream & ofs, Course courseCatalog[]){
for(int i=0; i<=getNumCourses(courseCatalog); i++){
ofs<<courseCatalog[i].credits<<
courseCatalog[i].name<<","<<
courseCatalog[i].id<<","<<
courseCatalog[i].professor<<","<<
courseCatalog[i].semester<<","<<
courseCatalog[i].grade<<endl;
}
}
Course findCourse(ifstream & ifs){
char attribute;
string needle;
int credits;
int name;
int id;
bool again=1;
int numLines = getNumLines(ifs);
cout<<"Search by (N)ame or (I)d? ";
cin>>attribute;
while(again){
switch(attribute){
case 'N': case 'n':
cout<<"Enter name of course: ";
cin>>name;
for(int i=0; i<numLines; i++){
}
break;
case 'I': case 'i':
cout<<"Enter course ID: ";
cin>>id;
break;
default:
cout<<"Invalid Attribute. Try Again."<<endl;
}
}
}
int getNumCourses(Course courseCatalog[]){
int numcourses=0;
for(int i=0;courseCatalog[i].credits!=0;i++){
numcourses++;
}
return numcourses;;
}
int getNumLines(ifstream & ifs){
char line[1];
int count=0;
while(ifs.getline(line, 1)){
count++;
}
ifs.close();
return count;
}
ostream& operator<<(ostream& os, const Course c){
os<<endl<<c.name<<endl<<c.id<<endl<<c.credits<<" cr hrs"<<endl;
return os;
}
#include "cc.h"
int displayMainMenu(){
int choice;
cout<<endl<<"**********MAIN MENU**********"<<endl;
cout<<"1. Print List Schedule"<<endl;
cout<<"2. Print Weekly Schedule"<<endl;
cout<<"3. Get Course"<<endl;
cout<<"4. Advanced Options"<<endl;
cout<<"0 to Quit"<<endl;
cout<<"*****************************"<<endl<<endl;
cout<<"Menu Choice: ";
cin>>choice;
return choice;
}
int displayAdvMenu(){
int choice;
cout<<endl<<"**********ADV MENU**********"<<endl;
cout<<"1. Add Course to Catalog"<<endl;
cout<<"2. Remove Duplicates"<<endl;
cout<<"0 to go Back"<<endl;
cout<<"****************************"<<endl<<endl;
cout<<"Menu Choice: ";
cin>>choice;
return choice;
}
Course addCourse(){
Course course;
cout<<"\nCourse name:\t\t";
cin.ignore();
cin.getline(course.name, CNAMESIZE);
cout<<"Course ID:\t\t";
cin.getline(course.id, CIDSIZE);
cout<<"Number of credits:\t";
cin>>course.credits;
/*<<"Enter semester: ";
cin.getline(course.semester);
for(int i=0; i<PREREQ_SIZE && choice==1; i++){
bool choice;
cout<<"Add PreReq?: ";
cin>>choice;
if(choice==1){
...
}
}
for(int i=0; i<POSTREQ_SIZE && choice==1; i++){
bool choice;
cout<<"Add PostReq?: ";
cin>>choice;
if(choice==1){
...
}
} */
return course;
}
void loadInCourses(ifstream & ifs, Course courseCatalog[]){
cout<<"b*"<<endl<<courseCatalog[0];
int i=0;
while(ifs>>courseCatalog[i].credits){
ifs.getline(courseCatalog[i].name, CNAMESIZE, ',');
ifs.getline(courseCatalog[i].id, CIDSIZE, ',');
ifs.getline(courseCatalog[i].professor, CPROFSIZE, ',');
ifs.getline(courseCatalog[i].semester, CSEMSIZE, ',');
ifs.get(courseCatalog[i].grade);
i++;
cout<<"d*"<<endl<<courseCatalog[i];
}
cout<<"c*"<<endl<<courseCatalog[0];
return;
}
void loadOutCourses(ofstream & ofs, Course courseCatalog[]){
for(int i=0; i<=getNumCourses(courseCatalog); i++){
ofs<<courseCatalog[i].credits<<
courseCatalog[i].name<<","<<
courseCatalog[i].id<<","<<
courseCatalog[i].professor<<","<<
courseCatalog[i].semester<<","<<
courseCatalog[i].grade<<endl;
}
}
Course findCourse(ifstream & ifs){
char attribute;
string needle;
int credits;
int name;
int id;
bool again=1;
int numLines = getNumLines(ifs);
cout<<"Search by (N)ame or (I)d? ";
cin>>attribute;
while(again){
switch(attribute){
case 'N': case 'n':
cout<<"Enter name of course: ";
cin>>name;
for(int i=0; i<numLines; i++){
}
break;
case 'I': case 'i':
cout<<"Enter course ID: ";
cin>>id;
break;
default:
cout<<"Invalid Attribute. Try Again."<<endl;
}
}
}
int getNumCourses(Course courseCatalog[]){
int numcourses=0;
for(int i=0;courseCatalog[i].credits!=0;i++){
numcourses++;
}
return numcourses;;
}
int getNumLines(ifstream & ifs){
char ch;
int count=0;
while(ch=ifs.get()){
count+=(ch=='\n'?1:0);
}
ifs.close();
return count;
}
ostream& operator<<(ostream& os, const Course c){
os<<endl<<c.name<<endl<<c.id<<endl<<c.credits<<" cr hrs"<<endl;
return os;
}
职能:
#include "cc.h"
int displayMainMenu(){
int choice;
cout<<endl<<"**********MAIN MENU**********"<<endl;
cout<<"1. Print List Schedule"<<endl;
cout<<"2. Print Weekly Schedule"<<endl;
cout<<"3. Get Course"<<endl;
cout<<"4. Advanced Options"<<endl;
cout<<"0 to Quit"<<endl;
cout<<"*****************************"<<endl<<endl;
cout<<"Menu Choice: ";
cin>>choice;
return choice;
}
int displayAdvMenu(){
int choice;
cout<<endl<<"**********ADV MENU**********"<<endl;
cout<<"1. Add Course to Catalog"<<endl;
cout<<"2. Remove Duplicates"<<endl;
cout<<"0 to go Back"<<endl;
cout<<"****************************"<<endl<<endl;
cout<<"Menu Choice: ";
cin>>choice;
return choice;
}
Course addCourse(){
Course course;
cout<<"\nCourse name:\t\t";
cin.ignore();
cin.getline(course.name, CNAMESIZE);
cout<<"Course ID:\t\t";
cin.getline(course.id, CIDSIZE);
cout<<"Number of credits:\t";
cin>>course.credits;
return course;
}
void loadInCourses(ifstream & ifs, Course courseCatalog[]){
int i=0;
while(ifs>>courseCatalog[i].credits){
ifs.getline(courseCatalog[i].name, CNAMESIZE, ',');
ifs.getline(courseCatalog[i].id, CIDSIZE, ',');
ifs.getline(courseCatalog[i].professor, CPROFSIZE, ',');
ifs.getline(courseCatalog[i].semester, CSEMSIZE, ',');
ifs.get(courseCatalog[i].grade);
i++;
}
return;
}
void loadOutCourses(ofstream & ofs, Course courseCatalog[]){
for(int i=0; i<=getNumCourses(courseCatalog); i++){
ofs<<courseCatalog[i].credits<<
courseCatalog[i].name<<","<<
courseCatalog[i].id<<","<<
courseCatalog[i].professor<<","<<
courseCatalog[i].semester<<","<<
courseCatalog[i].grade<<endl;
}
}
Course findCourse(ifstream & ifs){
char attribute;
string needle;
int credits;
int name;
int id;
bool again=1;
int numLines = getNumLines(ifs);
cout<<"Search by (N)ame or (I)d? ";
cin>>attribute;
while(again){
switch(attribute){
case 'N': case 'n':
cout<<"Enter name of course: ";
cin>>name;
for(int i=0; i<numLines; i++){
}
break;
case 'I': case 'i':
cout<<"Enter course ID: ";
cin>>id;
break;
default:
cout<<"Invalid Attribute. Try Again."<<endl;
}
}
}
int getNumCourses(Course courseCatalog[]){
int numcourses=0;
for(int i=0;courseCatalog[i].credits!=0;i++){
numcourses++;
}
return numcourses;;
}
int getNumLines(ifstream & ifs){
char line[1];
int count=0;
while(ifs.getline(line, 1)){
count++;
}
ifs.close();
return count;
}
ostream& operator<<(ostream& os, const Course c){
os<<endl<<c.name<<endl<<c.id<<endl<<c.credits<<" cr hrs"<<endl;
return os;
}
#include "cc.h"
int displayMainMenu(){
int choice;
cout<<endl<<"**********MAIN MENU**********"<<endl;
cout<<"1. Print List Schedule"<<endl;
cout<<"2. Print Weekly Schedule"<<endl;
cout<<"3. Get Course"<<endl;
cout<<"4. Advanced Options"<<endl;
cout<<"0 to Quit"<<endl;
cout<<"*****************************"<<endl<<endl;
cout<<"Menu Choice: ";
cin>>choice;
return choice;
}
int displayAdvMenu(){
int choice;
cout<<endl<<"**********ADV MENU**********"<<endl;
cout<<"1. Add Course to Catalog"<<endl;
cout<<"2. Remove Duplicates"<<endl;
cout<<"0 to go Back"<<endl;
cout<<"****************************"<<endl<<endl;
cout<<"Menu Choice: ";
cin>>choice;
return choice;
}
Course addCourse(){
Course course;
cout<<"\nCourse name:\t\t";
cin.ignore();
cin.getline(course.name, CNAMESIZE);
cout<<"Course ID:\t\t";
cin.getline(course.id, CIDSIZE);
cout<<"Number of credits:\t";
cin>>course.credits;
/*<<"Enter semester: ";
cin.getline(course.semester);
for(int i=0; i<PREREQ_SIZE && choice==1; i++){
bool choice;
cout<<"Add PreReq?: ";
cin>>choice;
if(choice==1){
...
}
}
for(int i=0; i<POSTREQ_SIZE && choice==1; i++){
bool choice;
cout<<"Add PostReq?: ";
cin>>choice;
if(choice==1){
...
}
} */
return course;
}
void loadInCourses(ifstream & ifs, Course courseCatalog[]){
cout<<"b*"<<endl<<courseCatalog[0];
int i=0;
while(ifs>>courseCatalog[i].credits){
ifs.getline(courseCatalog[i].name, CNAMESIZE, ',');
ifs.getline(courseCatalog[i].id, CIDSIZE, ',');
ifs.getline(courseCatalog[i].professor, CPROFSIZE, ',');
ifs.getline(courseCatalog[i].semester, CSEMSIZE, ',');
ifs.get(courseCatalog[i].grade);
i++;
cout<<"d*"<<endl<<courseCatalog[i];
}
cout<<"c*"<<endl<<courseCatalog[0];
return;
}
void loadOutCourses(ofstream & ofs, Course courseCatalog[]){
for(int i=0; i<=getNumCourses(courseCatalog); i++){
ofs<<courseCatalog[i].credits<<
courseCatalog[i].name<<","<<
courseCatalog[i].id<<","<<
courseCatalog[i].professor<<","<<
courseCatalog[i].semester<<","<<
courseCatalog[i].grade<<endl;
}
}
Course findCourse(ifstream & ifs){
char attribute;
string needle;
int credits;
int name;
int id;
bool again=1;
int numLines = getNumLines(ifs);
cout<<"Search by (N)ame or (I)d? ";
cin>>attribute;
while(again){
switch(attribute){
case 'N': case 'n':
cout<<"Enter name of course: ";
cin>>name;
for(int i=0; i<numLines; i++){
}
break;
case 'I': case 'i':
cout<<"Enter course ID: ";
cin>>id;
break;
default:
cout<<"Invalid Attribute. Try Again."<<endl;
}
}
}
int getNumCourses(Course courseCatalog[]){
int numcourses=0;
for(int i=0;courseCatalog[i].credits!=0;i++){
numcourses++;
}
return numcourses;;
}
int getNumLines(ifstream & ifs){
char ch;
int count=0;
while(ch=ifs.get()){
count+=(ch=='\n'?1:0);
}
ifs.close();
return count;
}
ostream& operator<<(ostream& os, const Course c){
os<<endl<<c.name<<endl<<c.id<<endl<<c.credits<<" cr hrs"<<endl;
return os;
}
标题:
#ifndef CC_H
#define CC_H
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
const int PREREQ_SIZE = 15;
const int POSTREQ_SIZE = 15;
const int CNAMESIZE=40;
const int CIDSIZE=15;
const int CPROFSIZE=40;
const int CSEMSIZE=15;
struct Course{
char name[CNAMESIZE];
char id[CIDSIZE];
int credits;
char professor[CPROFSIZE];
char semester[CSEMSIZE];
char grade;
float timeStart;
float timeStop;
friend ostream& operator<<(ostream& os, const Course c);
};
Course addCourse();
int displayMainMenu();
int displayAdvMenu();
void printString(Course c);
void loadInCourses(ifstream & ifs, Course courseCatalog[]);
void loadOutCourses(ofstream & ofs, Course courseCatalog[]);
Course findCourse(ifstream & ifs);
int getNumCourses(Course courseCatalog[]);
int getNumLines(ifstream & ifs);
#endif /* CC_H */
您的错误应该有两部分,无法从转换。。。还有 你是从……来的。。函数签名定义了要 这两者不一样。您必须更改函数签名以匹配传入的,或者修改传递签名的内容以匹配函数原型定义的 你会发现阅读整个错误是最有帮助的,你可能会认为发布整个错误是值得的,因为向我们展示部分错误几乎毫无意义。这会给你带来毫无意义的评论 如果您所追求的是一个指向任何数组的指针的函数,那么您可能应该这样称呼它:
Course (*courseCatalog)[]
或者简单地说:
课程*couseCatalog在cc.h中,这些行:
void loadInCourses(ifstream & ifs, Course courseCatalog);
void loadOutCourses(ofstream & ofs, Course courseCatalog);
应该是
void loadInCourses(ifstream & ifs, Course courseCatalog[]);
void loadOutCourses(ofstream & ofs, Course courseCatalog[]);
getNumLines中还有一个逻辑错误。如果istream::getline函数在到达行的末尾之前填充了缓冲区,则不会丢弃行的其余部分。您将在后续读取中获得该行的其余部分
相反,您可以只执行ifs.get并计算发生的次数
此函数用于关闭ifs。它只从findCourse调用,而findCourse还从未调用过。但是,当您调用findCourse时,请注意,如果要再次读取或重新打开文件,则需要将流倒回起始位置。哪一行出现错误?对我来说,它在loadInCourses和loadOutCourses fns的主文件中抛出错误。对我来说,它编译,你看到上面提供的链接了吗?你的编译器是什么?netbeans上的cygwin。你在用什么?你有一个非常奇怪的编译器,privatedata我不知道还有更多的错误。这就是ide给我的全部。我不想传递指针,我想传递数组本身。我对C++是相当新的,甚至还没有用指针来处理。我希望它都在数组对象中。你为什么要复制整个数组?你知道你的函数声明签名与你的函数定义签名不匹配吗?哦。。。我没有。这会导致那种错误吗?我现在不在电脑旁,所以我暂时无法参加考试。我肯定这是你的问题。修正标题。我没有意识到您确实正确定义了函数。声明被破坏了。嘿,谢谢你关于getNumLines的提示!你对ifstream了解很多吗?我最近才开始使用它,但每当我访问一个已经有内容的文件时,它就会被清除。我尝试将ifs模式设置为ios::app,但也不起作用。我会上传我更新的代码…如果流不应该清除文件,可能是发生了其他事情。正如我所说,我刚开始使用文件流,所以我想我应该先检查一下,我在loadInCourses之前添加了以下代码,它没有打印任何空值,所以我想我的问题在于fstreams字符行的声明[60];catalogIn.getLine,60;coutI刚刚注意到您对这两个文件使用了相同的文件名。您不应该这样做,即在不同的句柄上同时打开一个文件进行读取和写入。这可以解释你的症状。更新您的代码,以便在任何时候只有这两个流中的一个是打开的。