Creating Dynamic Object within a class.

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 886 Views - Last Post: 27 July 2012 - 03:35 PM Rate Topic: -----

#1 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Creating Dynamic Object within a class.

Posted 27 July 2012 - 10:11 AM

Firstly, here are my codes.

main.cpp
disk d1(7, "w"), d2;



disk.h
class disk {
	segment *s;
        int num;
public: 
	disk(int num_of_segments, const char *mode);
	disk();
	~disk();

};


disk.cpp
disk::disk(int num_of_segments, const char *mode) {
	num = num_of_segments;
	s = new segment[num];
}

disk::disk() {
	s = new segment[20];

}

disk::~disk() {	
	cout << "Deleting memory location: " << (void *) s << endl;
	cin.get();
	delete [ ] s;
}



When i try to compile it, why does it not dynamically allocate space for for segment? But instead i get

's' : undeclared identifier


I also get
syntax error : missing ';' before '*'
but i have checked over all my code and all the syntax seems correct.

Thanks!

Note: Class segment is in segment.h and everything works fine in that program.

This post has been edited by Sushii: 27 July 2012 - 10:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Creating Dynamic Object within a class.

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 5334
  • View blog
  • Posts: 16,609
  • Joined: 25-December 09

Re: Creating Dynamic Object within a class.

Posted 27 July 2012 - 10:24 AM

Post the complete error messages. What lines do these messages point to?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: Creating Dynamic Object within a class.

Posted 27 July 2012 - 10:43 AM

c:\xxxxx\disk.h(2): error C2143: syntax error : missing ';' before '*'
1>c:\xxxxx\disk.h(2): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\xxxxx\disk.h(2): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\xxxxx\disk.cpp(12): error C2065: 's' : undeclared identifier
1>c:\xxxxx\disk.cpp(27): error C2065: 's' : undeclared identifier
1>c:\xxxxx\disk.cpp(37): error C2065: 's' : undeclared identifier
1>c:\xxxxx\disk.cpp(39): error C2065: 's' : undeclared identifier
1>c:\xxxxx\disk.cpp(39): error C2541: 'delete' : cannot delete objects that are not pointers

This post has been edited by Sushii: 27 July 2012 - 10:43 AM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 5334
  • View blog
  • Posts: 16,609
  • Joined: 25-December 09

Re: Creating Dynamic Object within a class.

Posted 27 July 2012 - 10:48 AM

c:\xxxxx\disk.h(2): error C2143: syntax error : missing ';' before '*'

This looks very suspicious because it is on line #2.

Where are your include guards for your header? Every header file should have some kind of include guards to prevent multiple inclusion.

Where is your segment class defined? How does the compiler know where to locate this class? Did you perhaps forget to include the proper header file for this class?


Jim
Was This Post Helpful? 0
  • +
  • -

#5 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: Creating Dynamic Object within a class.

Posted 27 July 2012 - 10:52 AM

segment.h
#define MAX_STRING 10

class segment {
	char data[MAX_STRING][2000];
public:
	void initialize(const char core[][2000], int num);
	void initialize();
	int match(const char word[ ]) const;
	void sort();
	void get_word(char s[ ], int pos) const;
	int set_word(const char s[ ], int pos);
	int set_char(int word_pos, int char_pos, char value);
	char get_char(int word_pos, int char_pos) const;

	segment();
	segment(const char [][2000], int);
};


full disk.h
class disk {
	segment *s;
	int num;
	int size_max;
	char mmode[2];
public: 
	disk(int num_of_segments, const char *mode);
	disk();
	~disk();

};


full disk.cpp
#include <iostream>
#include <cstring>
#include <string>

#include "disk.h"
#include "segment.h"

using namespace std;

disk::disk(int num_of_segments, const char *mode) {
	num = num_of_segments;
	s = new segment[num];

	if (strcmp(mode, "a") == 0)
		strcpy(mmode, "a");
	else
		strcpy(mmode, "w");

	num = 0;
	size_max = num_of_segments;

	cout << "Mode is set to " << mmode << endl;
	cout << num_of_segments << " Segments Made" << endl;
}

disk::disk() {
	s = new segment[20];
	strcpy(mmode, "w");

	num = 0;
	size_max = 20;
	cout << "Mode is set to " << mmode << endl;
	cout << "20 Segments Made" << endl;
}

disk::~disk() {	
	cout << "Deleting memory location: " << (void *) s << endl;
	cin.get();
	delete [ ] s;
}



When i remove disk.cpp from the program, it works fine. So i dont think it could be a problem with segment.


Main also includes these headers
#include "disk.h"
#include "segment.h"

This post has been edited by Sushii: 27 July 2012 - 10:53 AM

Was This Post Helpful? 0
  • +
  • -

#6 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: Creating Dynamic Object within a class.

Posted 27 July 2012 - 11:03 AM

Header files should include guards like this:
#ifndef _SEGMENT_H
#define _SEGMENT_H
//code here
#endif


the same for disk , with the name changed of course .
Was This Post Helpful? 0
  • +
  • -

#7 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: Creating Dynamic Object within a class.

Posted 27 July 2012 - 11:08 AM

View PostTwoOfDiamonds, on 27 July 2012 - 12:03 PM, said:

Header files should include guards like this:
#ifndef _SEGMENT_H
#define _SEGMENT_H
//code here
#endif


the same for disk , with the name changed of course .


Added the include guards but oddly enough, i was never taught this.
Was This Post Helpful? 0
  • +
  • -

#8 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: Creating Dynamic Object within a class.

Posted 27 July 2012 - 11:10 AM

You should have been . It happened to me too when I first ran into OOP :)

So , now it works ?
Was This Post Helpful? 0
  • +
  • -

#9 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: Creating Dynamic Object within a class.

Posted 27 July 2012 - 11:14 AM

View PostTwoOfDiamonds, on 27 July 2012 - 12:10 PM, said:

You should have been . It happened to me too when I first ran into OOP :)

So , now it works ?


No, same errors. There was no error about repeating header files.
Was This Post Helpful? 0
  • +
  • -

#10 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: Creating Dynamic Object within a class.

Posted 27 July 2012 - 11:26 AM

In
disk::disk(int num_of_segments, const char *mode)
what is
const char *mode
used for ?

Never mind . I didn't see the full files
Was This Post Helpful? 0
  • +
  • -

#11 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: Creating Dynamic Object within a class.

Posted 27 July 2012 - 11:28 AM

View PostTwoOfDiamonds, on 27 July 2012 - 12:24 PM, said:

In
disk::disk(int num_of_segments, const char *mode)
what is
const char *mode
used for ?


Later on the program, it tells to program to append or write to a text file.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,114
  • Joined: 05-May 12

Re: Creating Dynamic Object within a class.

Posted 27 July 2012 - 11:29 AM

The guards still won't do anything because there is nothing to guard against. :lol:

Notice that disk.h uses the segment class, but he doesn't #include "segment.h".

He could mask the error, by swapping lines 5 and 6 in disk.cpp so that segment.h is included before disk.h, but the truly correct fix is to include "segment.h" in disk.h.
Was This Post Helpful? 0
  • +
  • -

#13 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: Creating Dynamic Object within a class.

Posted 27 July 2012 - 11:33 AM

I still got a lot to learn in order to spot mistakes faster >.<
Was This Post Helpful? 0
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: Creating Dynamic Object within a class.

Posted 27 July 2012 - 11:41 AM

An alternative to including segment.h in disk.h is a forward declaration of the segment class in disk.h.
Was This Post Helpful? 0
  • +
  • -

#15 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: Creating Dynamic Object within a class.

Posted 27 July 2012 - 11:42 AM

View PostJackOfAllTrades, on 27 July 2012 - 08:41 PM, said:

An alternative to including segment.h in disk.h is a forward declaration of the segment class.


I read in an article that this is better. And we should only include when we use a pointer :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2