473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Problem with Accessors

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?
Jan 13 '06 #1
16 2141
TB
Jordan Tiona sade:
<snip>
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
Jan 13 '06 #2
Jordan Tiona wrote:
[..]
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
Jan 13 '06 #3
Jordan Tiona wrote:
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 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.

Jan 13 '06 #4
New problem. I used strings now instead, and I can only input one word or
else it closes immediatly. How do I use cin.getline with strings.
Jan 13 '06 #5
TB
Jordan Tiona sade:
New problem. I used strings now instead, and I can only input one word or
else it closes immediatly. How do I use cin.getline with strings.


#include <string>
#include <iostream>

int main() {
std::string s;
std::getline(std::cin,s);
std::endl(std::cout);
std::cout<<s;
return 0;
}

TB
Jan 13 '06 #6

"TB" <TB@void.com> wrote in message
news:43***********************@taz.nntpserver.com. ..
Jordan Tiona sade:
New problem. I used strings now instead, and I can only input one word or
else it closes immediatly. How do I use cin.getline with strings.


#include <string>
#include <iostream>

int main() {
std::string s;
std::getline(std::cin,s);
std::endl(std::cout);
std::cout<<s;
return 0;
}

TB


I tried this:

cout << "Please enter information about your new CD.\n";
cout << "Album name: ";
cin.ignore();
cin.getline(&input, 25);
curData->SetAlbumName(input);
cout << "Artist name: ";
cin.ignore();
cin.getline(&input, 25);
curData->SetArtistName(input);
cout << "Year Released: ";
cin >> ipInt;
curData->SetYear(ipInt);
ignore();

And I'm getting an error:

c:\Documents and Settings\Jordan\My Documents\Visual Studio Projects\C++
Course\Workshop 1\CDDataBase\main.cpp(37): error C2664:
'std::basic_istream<_Elem,_Traits>::_Myt
&std::basic_istream<_Elem,_Traits>::getline(_El em *,std::streamsize)' :
cannot convert parameter 1 from 'std::string *__w64 ' to 'char *'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]

Are you sure that getline works with strings?
Jan 13 '06 #7
TB
Jordan Tiona sade:
"TB" <TB@void.com> wrote in message
news:43***********************@taz.nntpserver.com. ..
Jordan Tiona sade:
New problem. I used strings now instead, and I can only input one word or
else it closes immediatly. How do I use cin.getline with strings.

#include <string>
#include <iostream>

int main() {
std::string s;
std::getline(std::cin,s);
std::endl(std::cout);
std::cout<<s;
return 0;
}

TB


I tried this:

cout << "Please enter information about your new CD.\n";
cout << "Album name: ";
cin.ignore();
cin.getline(&input, 25);
curData->SetAlbumName(input);
cout << "Artist name: ";
cin.ignore();
cin.getline(&input, 25);
curData->SetArtistName(input);
cout << "Year Released: ";
cin >> ipInt;
curData->SetYear(ipInt);
ignore();

And I'm getting an error:

c:\Documents and Settings\Jordan\My Documents\Visual Studio Projects\C++
Course\Workshop 1\CDDataBase\main.cpp(37): error C2664:
'std::basic_istream<_Elem,_Traits>::_Myt
&std::basic_istream<_Elem,_Traits>::getline(_El em *,std::streamsize)' :
cannot convert parameter 1 from 'std::string *__w64 ' to 'char *'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]

Are you sure that getline works with strings?


First: I have no idea what 'input' is unless I read the error messages.
Second: I don't think you actually read my code.

I'm not using 'cin.getline()' but 'std::getline()' found in <string>.

TB
Jan 13 '06 #8
I'm a bit confused then. Your code is not clear. Please explain it a bit
better.
Jan 13 '06 #9
Never mind. I've got that figured out. Last problem... how do I check for
end of file?
Jan 13 '06 #10
TB
Jordan Tiona sade:
Never mind. I've got that figured out. Last problem... how do I check for
end of file?


#include <fstream>

int main() {
std::fstream f(...);
if(f.eof()) {
// end of file
}
return 0;
}

TB
Jan 13 '06 #11
Is there a way to do this with the stdio FILE struct?
Jan 13 '06 #12
Jordan Tiona schrieb:
Is there a way to do this with the stdio FILE struct?


Yes of course, but then you'll need a buffer char[<size>] and prevent
potential overflows of the buffer.

Regards, Stephan

Jan 13 '06 #13
Well, I found how to do it online. But now there is a problem with my
loading function. Could you possibly see what I'm doing? I apologize for
posting so many problems.

void Load(){
FILE* file;
string filename;
CDData* temp;
system("cls");

cout << "Name of file (without extension): ";
getline(cin, filename);
filename += ".dat";
cout << "\nLoading from " << filename;
curData = head;
if((file = fopen(filename.c_str(), "r+b"))!= NULL){
//We need to delete the old list first
//First, go to the last node
while(curData->GetNext()!= NULL)
curData = curData->GetNext();
//Then, we go through and delete each node, until we get to the head
while(curData->GetPrev()!= NULL){ //If it's NULL then this is head
curData = curData->GetPrev();
delete curData->GetNext();
}
//We keep the head node, and reset its next and prev
curData->SetNext(NULL);

//Now that we've deleted the list, we can make a new one.
while(!feof(file)){
temp = new CDData;
curData->SetNext(temp);
fread(temp, sizeof(temp), 1, file);
temp->SetPrev(curData);
curData = temp;
}
}
else
cout << "Error opening file";
}

Jan 13 '06 #14
Jordan Tiona schrieb:
Well, I found how to do it online. But now there is a problem with my
loading function. Could you possibly see what I'm doing? I apologize for
posting so many problems.

void Load(){
FILE* file;
string filename;
CDData* temp;
system("cls");

cout << "Name of file (without extension): ";
getline(cin, filename);
filename += ".dat";
cout << "\nLoading from " << filename;
curData = head;
if((file = fopen(filename.c_str(), "r+b"))!= NULL){
//We need to delete the old list first
//First, go to the last node
while(curData->GetNext()!= NULL)
curData = curData->GetNext();
//Then, we go through and delete each node, until we get to the head
while(curData->GetPrev()!= NULL){ //If it's NULL then this is head
curData = curData->GetPrev();
delete curData->GetNext();
}
//We keep the head node, and reset its next and prev
curData->SetNext(NULL);

//Now that we've deleted the list, we can make a new one.
while(!feof(file)){
feof(FILE*) returns true only after the EOF was read ...
temp = new CDData;
curData->SetNext(temp);
fread(temp, sizeof(temp), 1, file);
.... so fread will sooner or later fail.
(Even worse, in case of an error in `file' you could get
an endless loop because the "EOF"-flag is never set).

You can't read directly into temp: what is sizeof(temp) if
CDData::name is 1024 bytes long?
You must read/write each member indiviually
(thus my hint char buf[1024]).
temp->SetPrev(curData);
curData = temp;
}
}
else
cout << "Error opening file";
}


Better: provide input/output operator for CDData and use C++ streams.

Regards, Stephan

Jan 13 '06 #15

"Stephan Brönnimann" <br****@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Jordan Tiona schrieb:
Well, I found how to do it online. But now there is a problem with my
loading function. Could you possibly see what I'm doing? I apologize for
posting so many problems.

void Load(){
FILE* file;
string filename;
CDData* temp;
system("cls");

cout << "Name of file (without extension): ";
getline(cin, filename);
filename += ".dat";
cout << "\nLoading from " << filename;
curData = head;
if((file = fopen(filename.c_str(), "r+b"))!= NULL){
//We need to delete the old list first
//First, go to the last node
while(curData->GetNext()!= NULL)
curData = curData->GetNext();
//Then, we go through and delete each node, until we get to the head
while(curData->GetPrev()!= NULL){ //If it's NULL then this is head
curData = curData->GetPrev();
delete curData->GetNext();
}
//We keep the head node, and reset its next and prev
curData->SetNext(NULL);

//Now that we've deleted the list, we can make a new one.
while(!feof(file)){


feof(FILE*) returns true only after the EOF was read ...
temp = new CDData;
curData->SetNext(temp);
fread(temp, sizeof(temp), 1, file);


... so fread will sooner or later fail.
(Even worse, in case of an error in `file' you could get
an endless loop because the "EOF"-flag is never set).

You can't read directly into temp: what is sizeof(temp) if
CDData::name is 1024 bytes long?
You must read/write each member indiviually
(thus my hint char buf[1024]).
temp->SetPrev(curData);
curData = temp;
}
}
else
cout << "Error opening file";
}


Better: provide input/output operator for CDData and use C++ streams.

Regards, Stephan


What do you mean I/O operator for CDData? I'm sorry, I guess I'm more of a
newbie than I thought.
Jan 14 '06 #16
Jordan Tiona wrote:
What do you mean I/O operator for CDData? I'm sorry, I guess I'm more of a
newbie than I thought.


std::ostream& operator>>(std::ostream& os, const CDData& cdData);
and similar for the input operator of CDData to write/read CDData
to/from files.

Regards, Stephan

Jan 14 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Christopher J. Bottaro | last post by:
If I have the following class: class MyClass: def __init__(self): m_dict = {} m_dict = 1 m_dict = 2 m_dict = 3 Is there anyway to generate automatic accessors to the elements of the dict?
2
by: cody | last post by:
Does the clr allow more than one set and one get method for a property? Is it possible to use overloading for example set_Color(int c), set_Color(Color c)? from msdn: ...
10
by: Zap | last post by:
Widespread opinion is that public data members are evil, because if you have to change the way the data is stored in your class you have to break the code accessing it, etc. After reading this...
11
by: milkyway | last post by:
Hello, I have an HTML page that I am trying to import 2 .js file (I created) into. These files are: row_functions.js and data_check_functions.js. Whenever I bring the contents of the files into...
1
by: Brad Williams | last post by:
When I try to define accessors for this event, the event invocation line stops compiling. What's wrong? public class TestClass { public delegate int MyDelegate(string s); public event...
11
by: wASP | last post by:
Hi, I've got a pair of int properties in a class. The properties in question are indexing values - but that's not relevant to my problem - or it's just symptomatic ... sort of. They are...
7
by: none | last post by:
I'm trying to implement a simple repeateable property mechansism so I don't have to write accessors for every single instance variable I have. ------------ classMyObject: def __init__ (self):...
112
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them...
1
by: java4life | last post by:
Java problem -------------------------------------------------------------------------------- I need to generate the following output with a TestProgram. Lecturer 65374 has 3 students:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.