Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with Accessors

Reply
Thread Tools

Problem with Accessors

 
 
Jordan Tiona
Guest
Posts: n/a
 
      01-13-2006
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?


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-13-2006
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
 
Reply With Quote
 
 
 
 
TB
Guest
Posts: n/a
 
      01-13-2006
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
 
Reply With Quote
 
=?iso-8859-1?q?Stephan_Br=F6nnimann?=
Guest
Posts: n/a
 
      01-13-2006
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
Open source rating and billing engine for communication networks.

 
Reply With Quote
 
Jordan Tiona
Guest
Posts: n/a
 
      01-13-2006
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.


 
Reply With Quote
 
TB
Guest
Posts: n/a
 
      01-13-2006
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
 
Reply With Quote
 
Jordan Tiona
Guest
Posts: n/a
 
      01-13-2006

"TB" <(E-Mail Removed)> wrote in message
news:43c812a0$0$30401$(E-Mail Removed). ..
> 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?


 
Reply With Quote
 
TB
Guest
Posts: n/a
 
      01-13-2006
Jordan Tiona sade:
> "TB" <(E-Mail Removed)> wrote in message
> news:43c812a0$0$30401$(E-Mail Removed). ..
>> 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
 
Reply With Quote
 
Jordan Tiona
Guest
Posts: n/a
 
      01-13-2006
I'm a bit confused then. Your code is not clear. Please explain it a bit
better.


 
Reply With Quote
 
Jordan Tiona
Guest
Posts: n/a
 
      01-13-2006
Never mind. I've got that figured out. Last problem... how do I check for
end of file?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with accessors and dsl's Tsunami Scripter Ruby 2 01-29-2009 10:59 PM
accessors Mark A. Gibbs C++ 12 08-19-2004 04:16 PM
question about accessors and constants and variable declaration locations Flip Java 2 05-25-2004 02:28 PM
Do accessors lower performance? Zhao Java 3 10-11-2003 07:53 AM
Accessors by value or reference? Jarma C++ 0 08-29-2003 08:26 PM



Advertisments