Class members

J

Jim

I have made a class which includes some of the methods in the class
declaration, which is in the header;

slice.h
class Slice{
private:
float cdelt1,cdelt2;
float crval1,crval2;
float crpix1,crpix2;
int crval3;
string filename;
long ax1,ax2;
valarray<float> contents;
void readData();
public:
Slice(string fnm,int i);
float getCDelt1(){ return cdelt1;}
float getCDelt2(){ return cdelt2;}
float getCRPix1(){ return crpix1;}
float getCRPix2(){ return crpix2;}
float getCRVal1(){ return crval2;}
float getCRVal2(){ return crval2;}
int getCRVal3(){ return crval3;}
long getax1(){ return ax1;}
long getax2(){ return ax2;}
void copyData(valarray<float> & data, int i);
};

As these have already been declared in the class declaration I thought
they would have been included as class members but I get an error in
assemble when I try to compile the file using this class.
assemble.cpp:49: error: 'class std::slice' has no member named
'getCDelt1'
assemble.cpp:50: error: 'class std::slice' has no member named
'getCDelt2'
assemble.cpp:51: error: 'class std::slice' has no member named
'getCRVal1'
assemble.cpp:52: error: 'class std::slice' has no member named
'getCRVal2'
assemble.cpp:53: error: 'class std::slice' has no member named
'getCRPix1'
assemble.cpp:54: error: 'class std::slice' has no member named
'getCRPix2'
assemble.cpp:55: error: 'class std::slice' has no member named
'getax1'
assemble.cpp:56: error: 'class std::slice' has no member named
'getax2'
assemble.cpp:61: error: 'class std::slice' has no member named
'copyData'


I tried re-writing the names in the class declaration as e.g
Slice::copyData, but that didn't work. How should I be doing this?
Putting the class def in the header and commenting it out of the .cpp
has been the only way I can get multiline programs to compile. Also,
does anyone know an efficient C++ method to go from strings to floats
or doubles? At the moment I convert to c_str and use atof.

Thanks!
 
J

jamx

I have made a class which includes some of the methods in the class
declaration, which is in the header;

slice.h
class Slice{
private:
float cdelt1,cdelt2;
float crval1,crval2;
float crpix1,crpix2;
int crval3;
string filename;
long ax1,ax2;
valarray<float> contents;
void readData();
public:
Slice(string fnm,int i);
float getCDelt1(){ return cdelt1;}
float getCDelt2(){ return cdelt2;}
float getCRPix1(){ return crpix1;}
float getCRPix2(){ return crpix2;}
float getCRVal1(){ return crval2;}
float getCRVal2(){ return crval2;}
int getCRVal3(){ return crval3;}
long getax1(){ return ax1;}
long getax2(){ return ax2;}
void copyData(valarray<float> & data, int i);

};

As these have already been declared in the class declaration I thought
they would have been included as class members but I get an error in
assemble when I try to compile the file using this class.
assemble.cpp:49: error: 'class std::slice' has no member named
'getCDelt1'
assemble.cpp:50: error: 'class std::slice' has no member named
'getCDelt2'
assemble.cpp:51: error: 'class std::slice' has no member named
'getCRVal1'
assemble.cpp:52: error: 'class std::slice' has no member named
'getCRVal2'
assemble.cpp:53: error: 'class std::slice' has no member named
'getCRPix1'
assemble.cpp:54: error: 'class std::slice' has no member named
'getCRPix2'
assemble.cpp:55: error: 'class std::slice' has no member named
'getax1'
assemble.cpp:56: error: 'class std::slice' has no member named
'getax2'
assemble.cpp:61: error: 'class std::slice' has no member named
'copyData'

I tried re-writing the names in the class declaration as e.g
Slice::copyData, but that didn't work. How should I be doing this?
Putting the class def in the header and commenting it out of the .cpp
has been the only way I can get multiline programs to compile. Also,
does anyone know an efficient C++ method to go from strings to floats
or doubles? At the moment I convert to c_str and use atof.

Thanks!

it says std::
So your standard library already has a class named slice ;-) Try
something like ::slice in your assemble.cpp and see if that makes it
compile.
 
J

jamx

I have made a class which includes some of the methods in the class
declaration, which is in the header;

slice.h
class Slice{
private:
float cdelt1,cdelt2;
float crval1,crval2;
float crpix1,crpix2;
int crval3;
string filename;
long ax1,ax2;
valarray<float> contents;
void readData();
public:
Slice(string fnm,int i);
float getCDelt1(){ return cdelt1;}
float getCDelt2(){ return cdelt2;}
float getCRPix1(){ return crpix1;}
float getCRPix2(){ return crpix2;}
float getCRVal1(){ return crval2;}
float getCRVal2(){ return crval2;}
int getCRVal3(){ return crval3;}
long getax1(){ return ax1;}
long getax2(){ return ax2;}
void copyData(valarray<float> & data, int i);

};

As these have already been declared in the class declaration I thought
they would have been included as class members but I get an error in
assemble when I try to compile the file using this class.
assemble.cpp:49: error: 'class std::slice' has no member named
'getCDelt1'
assemble.cpp:50: error: 'class std::slice' has no member named
'getCDelt2'
assemble.cpp:51: error: 'class std::slice' has no member named
'getCRVal1'
assemble.cpp:52: error: 'class std::slice' has no member named
'getCRVal2'
assemble.cpp:53: error: 'class std::slice' has no member named
'getCRPix1'
assemble.cpp:54: error: 'class std::slice' has no member named
'getCRPix2'
assemble.cpp:55: error: 'class std::slice' has no member named
'getax1'
assemble.cpp:56: error: 'class std::slice' has no member named
'getax2'
assemble.cpp:61: error: 'class std::slice' has no member named
'copyData'

I tried re-writing the names in the class declaration as e.g
Slice::copyData, but that didn't work. How should I be doing this?
Putting the class def in the header and commenting it out of the .cpp
has been the only way I can get multiline programs to compile. Also,
does anyone know an efficient C++ method to go from strings to floats
or doubles? At the moment I convert to c_str and use atof.

Thanks!


btw, for converting you could try http://www.boost.org/libs/conversion/lexical_cast.htm
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

I have made a class which includes some of the methods in the class
declaration, which is in the header;

slice.h
class Slice{
assemble.cpp:49: error: 'class std::slice' has no member named
'getCDelt1'

Notice the difference in case of the first letter in the name of the
class, your class is named Slice but you are trying to use a class
called slice. Looks like you have 'using namespace std;' somewhere,
which is a bad habit.
 
M

Marcus Kwok

Jim said:
I have made a class which includes some of the methods in the class
declaration, which is in the header;

slice.h
class Slice{
private:
float cdelt1,cdelt2;
float crval1,crval2;
float crpix1,crpix2;
int crval3;
string filename;
long ax1,ax2;
valarray<float> contents;
void readData();
public:
Slice(string fnm,int i);
float getCDelt1(){ return cdelt1;}
float getCDelt2(){ return cdelt2;}
float getCRPix1(){ return crpix1;}
float getCRPix2(){ return crpix2;}
float getCRVal1(){ return crval2;}
float getCRVal2(){ return crval2;}
int getCRVal3(){ return crval3;}
long getax1(){ return ax1;}
long getax2(){ return ax2;}
void copyData(valarray<float> & data, int i);
};

As these have already been declared in the class declaration I thought
they would have been included as class members but I get an error in
assemble when I try to compile the file using this class.
assemble.cpp:49: error: 'class std::slice' has no member named
'getCDelt1'
[snip similar errors]

Note that C++ is case sensitive, so "slice" is different from "Slice".
Also, I see that you are using std::valarray, which has its own concept
of slice, so you must make sure that your Slice and the std::slice do
not conflict.
I tried re-writing the names in the class declaration as e.g
Slice::copyData, but that didn't work. How should I be doing this?
Putting the class def in the header and commenting it out of the .cpp
has been the only way I can get multiline programs to compile.

Try doing it like this:

// slice.h
#ifndef SLICE_H
#define SLICE_H

class Slice {
private:
float cdelt1, cdelt2;
float crval1, crval2;
// the rest are left as an exercise to the reader
void readData();
public:
// as Erik mentioned, you probably have a 'using namespace std;' in
// your header, which is not recommended; instead you should fully
// qualify the names in the header.
// Using it in your .cpp file is OK though.
Slice(std::string fnm, int i);
// any reason you're not passing by reference to const, like
// Slice(const std::string& fnm, int i);
// ?
};

#endif


// slice.cpp
#include "slice.h"

Slice::Slice(std::string fnm, int i)
: filename(fnm) // these are just a guess based on the names and
, crval3(i) // types of the variables
{
// any other initialization
}

void Slice::readData()
{
// implement the readData() function here
}
Also,
does anyone know an efficient C++ method to go from strings to floats
or doubles? At the moment I convert to c_str and use atof.

Well, some people have debated the efficiency of this method, but you
shouldn't really worry about that until you've determined that it is
indeed an issue. See this FAQ (and the surrounding ones too):

http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top