Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > int to size_t

Reply
Thread Tools

int to size_t

 
 
Gary Wessle
Guest
Posts: n/a
 
      07-21-2006
Hi

in the gsl, I am trying to use a function but getting this error

************************************************** **************
read_data.o: In function `read_data::matrix_the_file()':
read_data.cpp.text+0x4e): undefined reference to `read_data::gsl_matrix_alloc(unsigned int, unsigned int)'
read_data.cpp.text+0x8c): undefined reference to `gsl_matrix_fscanf'
Function: gsl_matrix * gsl_matrix_alloc (size_t n1, size_t n2)
************************************************** **************


the docs/example say
************************************************** **************
This function creates a matrix of size n1 rows by n2 columns,
returning a pointer to a newly initialized matrix struct. A new
block is allocated for the elements of the matrix, and stored in
the block component of the matrix struct. The block is owned by
the matrix, and will be deallocated when the matrix is
deallocated.
example given:
gsl_matrix * m = gsl_matrix_alloc (10, 3);
************************************************** **************

my code
**************** file.h ****************
class read_data
{
int nRows, nCol;
void No_of_Rows_Cols();
gsl_matrix * m;
gsl_matrix * gsl_matrix_alloc(size_t, size_t);
void matrix_the_file();

....
};

**************** file.cpp ****************
....
void read_data::matrix_the_file(){
// allocate memory for the matrix
// needs to be freed later using
// void gsl_matrix_free (gsl_matrix * m)
m = gsl_matrix_alloc (nRows, nCol); //<<------- here
FILE * f = fopen(file_name.c_str(), "rb");
gsl_matrix_fscanf (f, m);
fclose(f);

}
....
************************************************** **************
how can I case an int into size_t?

thanks
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-21-2006
Gary Wessle wrote:
> in the gsl,


I don't know what "gsl" is.

> I am trying to use a function but getting this error
>
> ************************************************** **************
> read_data.o: In function `read_data::matrix_the_file()':
> read_data.cpp.text+0x4e): undefined reference to
> `read_data::gsl_matrix_alloc(unsigned int, unsigned int)'
> read_data.cpp.text+0x8c): undefined reference to
> `gsl_matrix_fscanf'
> Function: gsl_matrix * gsl_matrix_alloc (size_t n1, size_t n2)
> ************************************************** **************


Undefined reference is usually a _linker_ error. Have you provided
the definition for those function to the linker?

> the docs/example say
> ************************************************** **************
> This function creates a matrix of size n1 rows by n2 columns,
> returning a pointer to a newly initialized matrix struct. A new
> block is allocated for the elements of the matrix, and stored in
> the block component of the matrix struct. The block is owned by
> the matrix, and will be deallocated when the matrix is
> deallocated.
> example given:
> gsl_matrix * m = gsl_matrix_alloc (10, 3);
> ************************************************** **************
>
> my code
> **************** file.h ****************
> class read_data
> {
> int nRows, nCol;
> void No_of_Rows_Cols();
> gsl_matrix * m;
> gsl_matrix * gsl_matrix_alloc(size_t, size_t);


This is the declaration of that function. Where is the definition?

> void matrix_the_file();
>
> ...
> };
>
> **************** file.cpp ****************
> ...
> void read_data::matrix_the_file(){
> // allocate memory for the matrix
> // needs to be freed later using
> // void gsl_matrix_free (gsl_matrix * m)
> m = gsl_matrix_alloc (nRows, nCol); //<<------- here
> FILE * f = fopen(file_name.c_str(), "rb");
> gsl_matrix_fscanf (f, m);
> fclose(f);
>
> }
> ...
> ************************************************** **************
> how can I case an int into size_t?


This has nothing to do with "casing" of an int into size_t. The linker
cannot find the definition of that function. Make sure you tell it where
to find the definition.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Gary Wessle
Guest
Posts: n/a
 
      07-21-2006
"Victor Bazarov" <(E-Mail Removed)> writes:

> Gary Wessle wrote:
> > in the gsl,

>
> I don't know what "gsl" is.
>
> > I am trying to use a function but getting this error
> >
> > ************************************************** **************
> > read_data.o: In function `read_data::matrix_the_file()':
> > read_data.cpp.text+0x4e): undefined reference to
> > `read_data::gsl_matrix_alloc(unsigned int, unsigned int)'
> > read_data.cpp.text+0x8c): undefined reference to
> > `gsl_matrix_fscanf'
> > Function: gsl_matrix * gsl_matrix_alloc (size_t n1, size_t n2)
> > ************************************************** **************

>
> Undefined reference is usually a _linker_ error. Have you provided
> the definition for those function to the linker?
>
> > the docs/example say
> > ************************************************** **************
> > This function creates a matrix of size n1 rows by n2 columns,
> > returning a pointer to a newly initialized matrix struct. A new
> > block is allocated for the elements of the matrix, and stored in
> > the block component of the matrix struct. The block is owned by
> > the matrix, and will be deallocated when the matrix is
> > deallocated.
> > example given:
> > gsl_matrix * m = gsl_matrix_alloc (10, 3);
> > ************************************************** **************
> >
> > my code
> > **************** file.h ****************
> > class read_data
> > {
> > int nRows, nCol;
> > void No_of_Rows_Cols();
> > gsl_matrix * m;
> > gsl_matrix * gsl_matrix_alloc(size_t, size_t);

>
> This is the declaration of that function. Where is the definition?
>
> > void matrix_the_file();
> >
> > ...
> > };
> >
> > **************** file.cpp ****************
> > ...
> > void read_data::matrix_the_file(){
> > // allocate memory for the matrix
> > // needs to be freed later using
> > // void gsl_matrix_free (gsl_matrix * m)
> > m = gsl_matrix_alloc (nRows, nCol); //<<------- here
> > FILE * f = fopen(file_name.c_str(), "rb");
> > gsl_matrix_fscanf (f, m);
> > fclose(f);
> >
> > }
> > ...
> > ************************************************** **************
> > how can I case an int into size_t?

>
> This has nothing to do with "casing" of an int into size_t. The linker
> cannot find the definition of that function. Make sure you tell it where
> to find the definition.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


I have in my header file
#include "gsl/gsl_matrix.h"

I then created a symlink to the directory where the library is.
$ ln -sf /usr/include/gsl/ myPrograms/common/

:~$ ls -l myPrograms/common/
total 32
lrwxrwxrwx 1 fred fred 17 2006-07-22 07:34 gsl -> /usr/include/gsl/
-rw-r--r-- 1 fred fred 234 2006-07-20 14:07 makefile
-rw-r-xr-- 1 fred fred 67 2006-07-21 11:57 #my.cpp#
-rw-r--r-- 1 fred fred 1000 2006-07-22 03:40 read_data.cpp
-rw-r--r-- 1 fred fred 379 2006-07-22 07:48 read_data.h
-rw-r--r-- 1 fred fred 4860 2006-07-22 07:35 read_data.o
-rw-r--r-- 1 fred fred 216 2006-07-21 19:56 read_data_test.cpp
-rw-r--r-- 1 fred fred 2380 2006-07-21 20:41 read_data_test.o

all this to avoid
1) add it to my compiler search path.
2) provide its location to the preprocessor in the makefile below

since I don't know how to do either.

 
Reply With Quote
 
Michael Taylor
Guest
Posts: n/a
 
      07-21-2006

"Gary Wessle" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Victor Bazarov" <(E-Mail Removed)> writes:
>
>> Gary Wessle wrote:
>> > in the gsl,

>>
>> I don't know what "gsl" is.
>>
>> > I am trying to use a function but getting this error
>> >
>> > ************************************************** **************
>> > read_data.o: In function `read_data::matrix_the_file()':
>> > read_data.cpp.text+0x4e): undefined reference to
>> > `read_data::gsl_matrix_alloc(unsigned int, unsigned int)'
>> > read_data.cpp.text+0x8c): undefined reference to
>> > `gsl_matrix_fscanf'
>> > Function: gsl_matrix * gsl_matrix_alloc (size_t n1, size_t n2)
>> > ************************************************** **************

>>
>> Undefined reference is usually a _linker_ error. Have you provided
>> the definition for those function to the linker?
>>
>> > the docs/example say
>> > ************************************************** **************
>> > This function creates a matrix of size n1 rows by n2 columns,
>> > returning a pointer to a newly initialized matrix struct. A new
>> > block is allocated for the elements of the matrix, and stored in
>> > the block component of the matrix struct. The block is owned by
>> > the matrix, and will be deallocated when the matrix is
>> > deallocated.
>> > example given:
>> > gsl_matrix * m = gsl_matrix_alloc (10, 3);
>> > ************************************************** **************
>> >
>> > my code
>> > **************** file.h ****************
>> > class read_data
>> > {
>> > int nRows, nCol;
>> > void No_of_Rows_Cols();
>> > gsl_matrix * m;
>> > gsl_matrix * gsl_matrix_alloc(size_t, size_t);

>>
>> This is the declaration of that function. Where is the definition?
>>
>> > void matrix_the_file();
>> >
>> > ...
>> > };
>> >
>> > **************** file.cpp ****************
>> > ...
>> > void read_data::matrix_the_file(){
>> > // allocate memory for the matrix
>> > // needs to be freed later using
>> > // void gsl_matrix_free (gsl_matrix * m)
>> > m = gsl_matrix_alloc (nRows, nCol); //<<------- here
>> > FILE * f = fopen(file_name.c_str(), "rb");
>> > gsl_matrix_fscanf (f, m);
>> > fclose(f);
>> >
>> > }
>> > ...
>> > ************************************************** **************
>> > how can I case an int into size_t?

>>
>> This has nothing to do with "casing" of an int into size_t. The linker
>> cannot find the definition of that function. Make sure you tell it where
>> to find the definition.
>>
>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask

>
> I have in my header file
> #include "gsl/gsl_matrix.h"
>
> I then created a symlink to the directory where the library is.
> $ ln -sf /usr/include/gsl/ myPrograms/common/
>
> :~$ ls -l myPrograms/common/
> total 32
> lrwxrwxrwx 1 fred fred 17 2006-07-22 07:34 gsl -> /usr/include/gsl/
> -rw-r--r-- 1 fred fred 234 2006-07-20 14:07 makefile
> -rw-r-xr-- 1 fred fred 67 2006-07-21 11:57 #my.cpp#
> -rw-r--r-- 1 fred fred 1000 2006-07-22 03:40 read_data.cpp
> -rw-r--r-- 1 fred fred 379 2006-07-22 07:48 read_data.h
> -rw-r--r-- 1 fred fred 4860 2006-07-22 07:35 read_data.o
> -rw-r--r-- 1 fred fred 216 2006-07-21 19:56 read_data_test.cpp
> -rw-r--r-- 1 fred fred 2380 2006-07-21 20:41 read_data_test.o
>
> all this to avoid
> 1) add it to my compiler search path.
> 2) provide its location to the preprocessor in the makefile below
>
> since I don't know how to do either.


I don't know what all that lrwxrwxrwx stuff is, but it looks like you
are trying to call a method (gsl_matrix_alloc) defined in some
library. But the way you've coded it you're trying to call a member
in read_data with the same name.

I'd try taking the declaration for gsl_matrix_alloc out of read_data.


 
Reply With Quote
 
Gary Wessle
Guest
Posts: n/a
 
      07-22-2006
"Michael Taylor" <(E-Mail Removed)> writes:

> "Gary Wessle" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > "Victor Bazarov" <(E-Mail Removed)> writes:
> >
> >> Gary Wessle wrote:
> >> > in the gsl,
> >>
> >> I don't know what "gsl" is.
> >>
> >> > I am trying to use a function but getting this error
> >> >
> >> > ************************************************** **************
> >> > read_data.o: In function `read_data::matrix_the_file()':
> >> > read_data.cpp.text+0x4e): undefined reference to
> >> > `read_data::gsl_matrix_alloc(unsigned int, unsigned int)'
> >> > read_data.cpp.text+0x8c): undefined reference to
> >> > `gsl_matrix_fscanf'
> >> > Function: gsl_matrix * gsl_matrix_alloc (size_t n1, size_t n2)
> >> > ************************************************** **************
> >>
> >> Undefined reference is usually a _linker_ error. Have you provided
> >> the definition for those function to the linker?
> >>
> >> > the docs/example say
> >> > ************************************************** **************
> >> > This function creates a matrix of size n1 rows by n2 columns,
> >> > returning a pointer to a newly initialized matrix struct. A new
> >> > block is allocated for the elements of the matrix, and stored in
> >> > the block component of the matrix struct. The block is owned by
> >> > the matrix, and will be deallocated when the matrix is
> >> > deallocated.
> >> > example given:
> >> > gsl_matrix * m = gsl_matrix_alloc (10, 3);
> >> > ************************************************** **************
> >> >
> >> > my code
> >> > **************** file.h ****************
> >> > class read_data
> >> > {
> >> > int nRows, nCol;
> >> > void No_of_Rows_Cols();
> >> > gsl_matrix * m;
> >> > gsl_matrix * gsl_matrix_alloc(size_t, size_t);
> >>
> >> This is the declaration of that function. Where is the definition?
> >>
> >> > void matrix_the_file();
> >> >
> >> > ...
> >> > };
> >> >
> >> > **************** file.cpp ****************
> >> > ...
> >> > void read_data::matrix_the_file(){
> >> > // allocate memory for the matrix
> >> > // needs to be freed later using
> >> > // void gsl_matrix_free (gsl_matrix * m)
> >> > m = gsl_matrix_alloc (nRows, nCol); //<<------- here
> >> > FILE * f = fopen(file_name.c_str(), "rb");
> >> > gsl_matrix_fscanf (f, m);
> >> > fclose(f);
> >> >
> >> > }
> >> > ...
> >> > ************************************************** **************
> >> > how can I case an int into size_t?
> >>
> >> This has nothing to do with "casing" of an int into size_t. The linker
> >> cannot find the definition of that function. Make sure you tell it where
> >> to find the definition.
> >>
> >> V
> >> --
> >> Please remove capital 'A's when replying by e-mail
> >> I do not respond to top-posted replies, please don't ask

> >
> > I have in my header file
> > #include "gsl/gsl_matrix.h"
> >
> > I then created a symlink to the directory where the library is.
> > $ ln -sf /usr/include/gsl/ myPrograms/common/
> >
> > :~$ ls -l myPrograms/common/
> > total 32
> > lrwxrwxrwx 1 fred fred 17 2006-07-22 07:34 gsl -> /usr/include/gsl/
> > -rw-r--r-- 1 fred fred 234 2006-07-20 14:07 makefile
> > -rw-r-xr-- 1 fred fred 67 2006-07-21 11:57 #my.cpp#
> > -rw-r--r-- 1 fred fred 1000 2006-07-22 03:40 read_data.cpp
> > -rw-r--r-- 1 fred fred 379 2006-07-22 07:48 read_data.h
> > -rw-r--r-- 1 fred fred 4860 2006-07-22 07:35 read_data.o
> > -rw-r--r-- 1 fred fred 216 2006-07-21 19:56 read_data_test.cpp
> > -rw-r--r-- 1 fred fred 2380 2006-07-21 20:41 read_data_test.o
> >
> > all this to avoid
> > 1) add it to my compiler search path.
> > 2) provide its location to the preprocessor in the makefile below
> >
> > since I don't know how to do either.

>
> I don't know what all that lrwxrwxrwx stuff is, but it looks like you
> are trying to call a method (gsl_matrix_alloc) defined in some
> library. But the way you've coded it you're trying to call a member
> in read_data with the same name.
>
> I'd try taking the declaration for gsl_matrix_alloc out of read_data.



I am getting
************************************************** **************
fred@debian:~/myPrograms/common$ make
g++ -c -o read_data.o read_data.cpp
g++ read_data.o read_data_test.o -o proj
read_data.o: In function `read_data::matrix_the_file()':
read_data.cpp.text+0x47): undefined reference to `gsl_matrix_alloc'
read_data.cpp.text+0x85): undefined reference to `gsl_matrix_fscanf'
collect2: ld returned 1 exit status
make: *** [proj] Error 1
fred@debian:~/myPrograms/common$
************************************************** **************

the files are

**************** read_data.h ****************
#ifndef READ_DATA_H
#define READ_DATA_H
#include <string>
#include <cstdio>
#include "gsl/gsl_matrix.h"
class read_data
{
int nRows, nCol;
std::string file_name;
void No_of_Rows_Cols();
gsl_matrix * m;
void matrix_the_file();

public:
read_data(std::string const& fileName);
~read_data();

};


#endif



**************** read_data.cpp ****************
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include "gsl/gsl_matrix.h"
#include "read_data.h"

using namespace std;

read_data::read_data( string const& fileName )
: file_name( fileName ) {
set_No_of_Rows_Cols();
matrix_the_file();
}

read_data::~read_data() {}

void read_data::matrix_the_file(){
// allocate memory for the matrix
// needs to be freed later using
// void gsl_matrix_free (gsl_matrix * m)
m = gsl_matrix_alloc (nRows, nCol);
FILE * f = fopen(file_name.c_str(), "rb");
gsl_matrix_fscanf (f, m);
fclose(f);

}

void read_data::set_No_of_Rows_Cols() {
ifstream in(file_name.c_str());
string line;
getline(in, line);
stringstream input( line.c_str() );

string word;
nCol = 0;
while(input >> word)
nCol++;

nRows = 1;
while (getline(in, line))
nRows++;
}

/*
another solution to use system fun. output
std::string command = "wc -l";
std::system( ( command + " " + file_name ).c_str() );
}
*/


**************** read_data_test.cpp ****************
#include <fstream>
#include <iostream>
#include <string>
#include "read_data.h"

using namespace std;

int main() {
string f = "../../data/ZB/Jun06/20060405";
read_data data1( f ); // space delimited


}



**************** makefile ****************
OBJS = read_data.o read_data_test.o
COMP = g++

#### linker section ####
proj: $(OBJS)
$(COMP) $(OBJS) -o proj
########################


#### compiler section ####
..SUFFIXES:.o .cpp .h
..h.o:
$(COMP) -c $<
##########################
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-24-2006
Gary Wessle wrote:
> [...]
> I am getting
> ************************************************** **************
> fred@debian:~/myPrograms/common$ make
> g++ -c -o read_data.o read_data.cpp
> g++ read_data.o read_data_test.o -o proj
> read_data.o: In function `read_data::matrix_the_file()':
> read_data.cpp.text+0x47): undefined reference to `gsl_matrix_alloc'


OK, let's just take this one function, 'gsl_matrix_alloc'.

> read_data.cpp.text+0x85): undefined reference to `gsl_matrix_fscanf'
> collect2: ld returned 1 exit status
> make: *** [proj] Error 1
> fred@debian:~/myPrograms/common$
> ************************************************** **************
>
> the files are
> [...]
>
> **************** read_data.cpp ****************
> [...]
> void read_data::matrix_the_file(){
> // allocate memory for the matrix
> // needs to be freed later using
> // void gsl_matrix_free (gsl_matrix * m)
> m = gsl_matrix_alloc (nRows, nCol);


I see it *used* here...

> FILE * f = fopen(file_name.c_str(), "rb");
> gsl_matrix_fscanf (f, m);
> fclose(f);
>
> }
> [...]


But not *defined* anywhere in _your_ program. Why? Becuase, as you might
have mentioned, it comes from a *library*. You need to supply that library
to your *linker* at the link stage. Learn how to do it (because it's OT
here to discuss any particular compiler's command-line options).

A hint: the command line you supply

> g++ read_data.o read_data_test.o -o proj


is probably missing something. It seems to have no libraries at all, only
your own object files.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Jacek Dziedzic
Guest
Posts: n/a
 
      07-24-2006

>>>>>I am trying to use a function but getting this error
>>>>>
>>>>>********************************************* *******************
>>>>>read_data.o: In function `read_data::matrix_the_file()':
>>>>>read_data.cpp.text+0x4e): undefined reference to
>>>>>`read_data::gsl_matrix_alloc(unsigned int, unsigned int)'
>>>>>read_data.cpp.text+0x8c): undefined reference to
>>>>>`gsl_matrix_fscanf'
>>>>>Function: gsl_matrix * gsl_matrix_alloc (size_t n1, size_t n2)
>>>>>********************************************* *******************
>>>>
>>>>Undefined reference is usually a _linker_ error. Have you provided
>>>>the definition for those function to the linker?
>>>>
>>>


> [...]


This looks like a linker error -- you told your compiler
you have a gsl_matrix_alloc function, but forgot to tell it
where to find it (its definition).

As Victor already pointed out, learning how to operate
your compiler/linker is beyond the scope of this group,
try your man pages (<OT> hint: look for the options -l and -L </OT>).


>>>1) add it to my compiler search path.
>>>2) provide its location to the preprocessor in the makefile below
>>>
>>>since I don't know how to do either.


So it seems. <eg>

> [snip]


HTH,
- J.
 
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
Re: int vs. unsigned int vs. size_t Ian Collins C Programming 2 04-30-2011 10:37 PM
Re: int vs. unsigned int vs. size_t Tim Prince C Programming 3 04-30-2011 10:34 PM
When should I use ptrdiff_t and size_t instead of int and unsigned int? PengYu.UT@gmail.com C++ 6 10-19-2005 01:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments