what mistake I made in this head file?

D

Daqian Yang

here is the code:
---
#ifndef XFILE_A2_H
#define XFILE_A2_H

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

namespace xf{
class Xfile{

public:
void compress();// throw (runtime_error);
void stats();// throw (runtime_error);

private:
Xfile(string = "mydata", int = 10, int = 10); //constructor
typedef char* Xstr;
Xstr* storage_; //an array of array to store the data
int size_; //lines array contains
int length_; //length for each line
}
}
#endif

----

GCC gives me this error:
xfile_a2.h:23: error: parse error before `}' token
xfile_a2.h: In function `int xf::main()':
xfile_a2.h:17: error: `xf::Xfile::Xfile(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int, int)' is private

anyone knows why(maybe a newbie question)? Thanks.
 
M

Michael Mellor

Daqian said:
here is the code:
---
#ifndef XFILE_A2_H
#define XFILE_A2_H

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

namespace xf{
class Xfile{

public:
void compress();// throw (runtime_error);
void stats();// throw (runtime_error);

private:
Xfile(string = "mydata", int = 10, int = 10); //constructor
typedef char* Xstr;
Xstr* storage_; //an array of array to store the data
int size_; //lines array contains
int length_; //length for each line
} };

}
#endif
Fixed by adding the semi-colon.
xfile_a2.h: In function `int xf::main()':
xfile_a2.h:17: error: `xf::Xfile::Xfile(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int, int)' is private
I would need to see xf::main to tell the exact problem. However the only
constructor for Xfile is private and therefore you will struggle to
create an Xfile object. The only why an Xfile object could be created is
by: a static Xfile method and there isn't one; a friend class and once
again there isn't one; or by a method of Xfile but due to the previous
facts there will not be a way to call a method of Xfile (chicken and egg
problem).

Michael Mellor
 
D

Daqian Yang

After I modified the program like this it has no compiling error:

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

namespace xf{
class Xfile{

public:
void compress();// throw (runtime_error);
void stats();// throw (runtime_error);

private:
Xfile(string = "mydata", int = 10, int = 10); //constructor
typedef char* Xstr;
Xstr* storage_; //an array of array to store the data
int size_; //lines array contains
int length_; //length for each line
};
};
 
M

Michael Mellor

Daqian said:
After I modified the program like this it has no compiling error:

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

namespace xf{
class Xfile{

public:
void compress();// throw (runtime_error);
void stats();// throw (runtime_error);

private:
Xfile(string = "mydata", int = 10, int = 10); //constructor
typedef char* Xstr;
Xstr* storage_; //an array of array to store the data
int size_; //lines array contains
int length_; //length for each line
};
};
In simple terms you can write:
class a {
public:
int f ( );
} an_object;

void func() {
an_object.f()
}
which defines a class of type 'a' and create an object 'an_object' of
type 'a'. So if you don't want to also create an object of type 'a' when
defining it you need:

class a {
public:
int f ( );
};

Dont't put semi colons after namespaces as it is standard although g++
(and I guess many other compilers) allow it:

namespace xf{
class Xfile{
......
}; // ; does go here
} // No ; here

Michael Mellor
 
D

Derek

Daqian said:
After I modified the program like this it has no compiling error:

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

namespace xf{
class Xfile{

public:
void compress();// throw (runtime_error);
void stats();// throw (runtime_error);

private:
Xfile(string = "mydata", int = 10, int = 10); //constructor
typedef char* Xstr;
Xstr* storage_; //an array of array to store the data
int size_; //lines array contains
int length_; //length for each line
};
};

A semicolon is required after a class declaration:

class Xfile
{
....
}; // Notice the semicolon

You left the semicolon out in your original version and the
compiler complained. A semicolon is NOT required when closing
a namespace:

namespace xf
{
....
} // No semicolon needed here, though you use one
 
R

red floyd

Daqian said:
After I modified the program like this it has no compiling error:

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

namespace xf{
class Xfile{

public:
void compress();// throw (runtime_error);
void stats();// throw (runtime_error);

private:
Xfile(string = "mydata", int = 10, int = 10); //constructor
typedef char* Xstr;
Xstr* storage_; //an array of array to store the data
int size_; //lines array contains
int length_; //length for each line
};
};

because you need a semicolon after the class definition. BTW, you don't
need one after the namespace definition.
 
K

Kiel W.

Daqian Yang said:
After I modified the program like this it has no compiling error:

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

namespace xf{
class Xfile{

public:
void compress();// throw (runtime_error);
void stats();// throw (runtime_error);

private:
Xfile(string = "mydata", int = 10, int = 10); //constructor
typedef char* Xstr;
Xstr* storage_; //an array of array to store the data
int size_; //lines array contains
int length_; //length for each line
};
};


When you declare a class you MUST end it with a ;

class Foo
{
..whatever in here..
}; // This is important to have!

I believe this is the case with namespaces also. Remove the ; on the
last } and see if you get your error back (as you should)
 
D

Derek

Kiel said:
When you declare a class you MUST end it with a ;

class Foo { ..whatever in here.. }; // This is important to have!

I believe this is the case with namespaces also. Remove
the ; on the last } and see if you get your error back (as
you should)

No, namespaces do not require the semicolon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top