访问者问题 [英] Problem with Accessors

查看:38
本文介绍了访问者问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码。


main.cpp:


#include" cddb.h"


CDData * head;

CDData * curData;


int main(){


head = new CDData();

curData = head;


EnterInfo();

Print();

返回0;

}


无效EnterInfo(){

CDData * temp;

char * input = new char();

int ipInt;


system(" cls");


temp = new CDData();

curData-> SetNext(temp);

curData = temp;


cout<< 请输入有关新CD的信息。\ nn;

cout<< " \ nAlbum name:" ;;

cin.getline(input,25);

curData-> SetAlbumName(input);

cout<< \艺术家姓名:" ;;

cin.getline(输入,25);

curData-> SetArtistName(输入);

cout<< " \ nYear发布:" ;;

cin>> ipInt;

curData-> SetYear(ipInt);

}


void Print(){

int i = 1;

curData = head;


while(true){

curData = curData-> ; GetNext();

if(curData = NULL)

break;


cout<< CD# << i<< endl;

cout<< 专辑名称: << curData-> GetAlbumName()<< endl;

cout<< 艺术家姓名: << curData-> GetArtistName()<< endl;

cout<< 发布年份: << curData-> GetYear()<< endl;

cout<< " ------------------------------------------- \\\
\ n" ;;


i ++;

}

}


cddb.h :


#include< iostream>

#include< string>

using namespace std;


void EnterInfo();

void保存();

void Load();

void Print() ;


class CDData {

private:

CDData * next; //对于链表

char * albumName;

char * artistName;

int year;

public:

//构造函数/析构函数

CDData(){this-> SetNext(NULL);}

~CDData(){}

//访问者

char * GetAlbumName(){return this-> albumName;}

char * GetArtistName(){return this-> ; artistName;}

int GetYear(){return year;}

CDData * GetNext(){return next;}


void SetAlbumName(char * newName){albumName = newName;}

void SetArtistName(char * newName){artistName = newName;}

void SetYear(int newYear){ year = newYear;}

void SetNext(CDData * newNext){next = newNext;}

};


我'在打印功能运行期间出现访问冲突错误,当我试图使用GetXXX访问器时,



0x0041fc46处的未处理异常在CDDataBase.exe中:0xC0000005:访问

违规读取locati在0x00000004。


我做错了什么?

Here is my code.

main.cpp:

#include "cddb.h"

CDData* head;
CDData* curData;

int main(){

head = new CDData();
curData = head;

EnterInfo();
Print();
return 0;
}

void EnterInfo(){
CDData* temp;
char* input = new char();
int ipInt;

system("cls");

temp = new CDData();
curData->SetNext(temp);
curData = temp;

cout << "Please enter information about your new CD.\n";
cout << "\nAlbum name: ";
cin.getline(input, 25);
curData->SetAlbumName(input);
cout << "\nArtist name: ";
cin.getline(input, 25);
curData->SetArtistName(input);
cout << "\nYear Released: ";
cin >> ipInt;
curData->SetYear(ipInt);
}

void Print(){
int i = 1;
curData = head;

while(true){
curData = curData->GetNext();
if(curData = NULL)
break;

cout << "CD #" << i << endl;
cout << "Album Name: " << curData->GetAlbumName() << endl;
cout << "Artist Name: " << curData->GetArtistName() << endl;
cout << "Year Released: " << curData->GetYear() << endl;
cout << "-------------------------------------------\n\n";

i++;
}
}

cddb.h:

#include <iostream>
#include <string>
using namespace std;

void EnterInfo();
void Save();
void Load();
void Print();

class CDData {
private:
CDData *next; //For linked list
char* albumName;
char* artistName;
int year;
public:
//Constructor/Destructor
CDData(){ this->SetNext(NULL);}
~CDData(){}
//Accessors
char* GetAlbumName(){return this->albumName;}
char* GetArtistName(){return this->artistName;}
int GetYear(){return year;}
CDData* GetNext(){return next;}

void SetAlbumName(char* newName){albumName = newName;}
void SetArtistName(char* newName){artistName = newName;}
void SetYear(int newYear){year = newYear;}
void SetNext(CDData* newNext){next = newNext;}
};

I''m getting an access violation error during runtime at the print function,
when I''m trying to use the GetXXX accessors.

Unhandled exception at 0x0041fc46 in CDDataBase.exe: 0xC0000005: Access
violation reading location 0x00000004.

What am I doing wrong?

推荐答案

Jordan Tiona sade:

< snip>
Jordan Tiona sade:
<snip>
while(true){
curData = curData-> GetNext();
if(curData = NULL)


if(curData == NULL)

break;

< snip>
我在访问期间遇到访问冲突错误打印功能的运行时,
当我试图使用GetXXX访问器时。

CDDataBase.exe中0x0041fc46处的未处理异常:0xC0000005:访问
违规读取位置0x00000004 。

我做错了什么?
while(true){
curData = curData->GetNext();
if(curData = NULL)
if(curData == NULL)
break;
<snip>
I''m getting an access violation error during runtime at the print function,
when I''m trying to use the GetXXX accessors.

Unhandled exception at 0x0041fc46 in CDDataBase.exe: 0xC0000005: Access
violation reading location 0x00000004.

What am I doing wrong?




TB



TB


Jordan Tiona写道:
Jordan Tiona wrote:
[..]
void Print(){
int i = 1;
curData = head;

while(true ){
curData = curData-> GetNext();
if(curData = NULL)


比较运算符是''=='',不是 =。在这里你只需要为''curData''指定NULL



休息;
[...]
[..]
void Print(){
int i = 1;
curData = head;

while(true){
curData = curData->GetNext();
if(curData = NULL)
Comparison operator is ''=='', not ''=''. Here you just assigned NULL
to ''curData''.
break;
[...]



V



V


Jordan Tiona写道:
Jordan Tiona wrote:
这是我的代码。

main.cpp:

#include" cddb.h"

CDData * head;
CDData * curData;

int main(){< br =>
head = new CDData();
curData = head;

EnterInfo();
Print();
返回0;
}

void EnterInfo(){
CDData * temp;
char * input = new char();
int ipInt;

system(cls);

temp = new CDData();
curData-> SetNext(temp);
curData = temp;

cout<< 请输入有关新CD的信息。\ n;;
cout<< " \ nAlbum name:" ;;
cin.getline(input,25);
curData-> SetAlbumName(input);
cout<< \艺术家姓名:" ;;
cin.getline(输入,25);
curData-> SetArtistName(输入);
cout<< \ nYear发布:" ;;
cin>> ipInt;
curData-> SetYear(ipInt);
}

void Print(){
int i = 1;
curData = head;

while(true){
curData = curData-> GetNext();
if(curData = NULL)
break;

cout<< CD# << i<< endl;
cout<< 专辑名称: << curData-> GetAlbumName()<< endl;
cout<< 艺术家姓名: << curData-> GetArtistName()<< endl;
cout<< 发布年份: << curData-> GetYear()<< endl;
cout<< " ------------------------------------------- \\\
\ n;

i ++;
}


cddb.h:

#include< iostream>
#include< string>
使用命名空间std;

void EnterInfo();
void Save();
void Load();
void Print();

class CDData {
private:
CDData * next; //对于链接列表
char * albumName;
char * artistName;
int year;
public:
//构造函数/析构函数
CDData() {this-> SetNext(NULL);}
~CDData(){}
//访问者
char * GetAlbumName(){return this-> albumName;}
char * GetArtistName(){return this-> artistName;}
int GetYear(){return year;}
CDData * GetNext(){return next;}

void SetAlbumName(char * newName){albumName = newName;}
void SetArtistName(char * newName){artistName = newName;}
void SetYear(int newYear){year = newYear;}
void SetNext(CDData * newNext){next = newNext;}
};

我在打印功能运行期间遇到访问冲突错误,
当我''我试图使用GetXXX访问器。

CDDataBase.exe中0x0041fc46处理未处理的异常:0xC0000005:访问
违规读取位置0x00000004。

我做错了什么?
Here is my code.

main.cpp:

#include "cddb.h"

CDData* head;
CDData* curData;

int main(){

head = new CDData();
curData = head;

EnterInfo();
Print();
return 0;
}

void EnterInfo(){
CDData* temp;
char* input = new char();
int ipInt;

system("cls");

temp = new CDData();
curData->SetNext(temp);
curData = temp;

cout << "Please enter information about your new CD.\n";
cout << "\nAlbum name: ";
cin.getline(input, 25);
curData->SetAlbumName(input);
cout << "\nArtist name: ";
cin.getline(input, 25);
curData->SetArtistName(input);
cout << "\nYear Released: ";
cin >> ipInt;
curData->SetYear(ipInt);
}

void Print(){
int i = 1;
curData = head;

while(true){
curData = curData->GetNext();
if(curData = NULL)
break;

cout << "CD #" << i << endl;
cout << "Album Name: " << curData->GetAlbumName() << endl;
cout << "Artist Name: " << curData->GetArtistName() << endl;
cout << "Year Released: " << curData->GetYear() << endl;
cout << "-------------------------------------------\n\n";

i++;
}
}

cddb.h:

#include <iostream>
#include <string>
using namespace std;

void EnterInfo();
void Save();
void Load();
void Print();

class CDData {
private:
CDData *next; //For linked list
char* albumName;
char* artistName;
int year;
public:
//Constructor/Destructor
CDData(){ this->SetNext(NULL);}
~CDData(){}
//Accessors
char* GetAlbumName(){return this->albumName;}
char* GetArtistName(){return this->artistName;}
int GetYear(){return year;}
CDData* GetNext(){return next;}

void SetAlbumName(char* newName){albumName = newName;}
void SetArtistName(char* newName){artistName = newName;}
void SetYear(int newYear){year = newYear;}
void SetNext(CDData* newNext){next = newNext;}
};

I''m getting an access violation error during runtime at the print function,
when I''m trying to use the GetXXX accessors.

Unhandled exception at 0x0041fc46 in CDDataBase.exe: 0xC0000005: Access
violation reading location 0x00000004.

What am I doing wrong?



CDData :: albumName a nd CCData :: artistName永远不会被初始化。

如果你坚持使用char *,你必须实现复制构造函数,

赋值运算符和析构函数。


如果使用std :: string而不是

char *,大多数问题都会解决。

提示:如果你想要一个25个字符的缓冲区使用

char * buf = new char [25];

并且不要忘记稍后删除它

delete [] buf;


为什么宣布

+ void EnterInfo();

+ void Save();

+ void Load();

+ void Print();
如果在main.cpp中实现了cddb.h中的


你真的希望每个使用CDData

的应用程序都必须提供它们吗?


问候,Stephan
br **** @ osb-systems.com

通信网络的开源评级和计费引擎。


CDData::albumName and CCData::artistName are never initialized.
If you stick with char*, you must implement the copy constructor,
assignment operator and destructor.

Most of your problems will solve if you use std::string instead of
char*.
Hint: if you want a buffer of 25 characters use
char* buf = new char[25];
and don''t forget to delete it later
delete [] buf;

Why do you declare
+ void EnterInfo();
+ void Save();
+ void Load();
+ void Print();
in cddb.h if there are implemented in main.cpp.
Do you really want that every application that uses CDData
has to provide them?

Regards, Stephan
br****@osb-systems.com
Open source rating and billing engine for communication networks.


这篇关于访问者问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆