monkey_king 39 Junior Poster

Nice class,
I like you added a diff and eval function.

Maybe you should update it,
with inline operators +-*<<

monkey_king 39 Junior Poster

If you can't do something like this, try using strtok, it will help you split up the word using a delimiter.

Hope this helps.

Yes a tokenizer is highly recommended.
You will end up using it in half you projects.

monkey_king 39 Junior Poster

I wouldnt bother with the exponent.
I would only use the coefficient and let the position in an "Array" be the exponent.

Exponents are by definition integers >0 ,
so it should be ideally suited for a simple array.

the polynomial
[TEX]a Z^4 -b Z^3+cZ+7[/TEX]
the last term has an invisble z^0
so it should be represented by

{7,c,0,-b,a}

sum would just be the placewise addition,
prod is somewhat more involved, but you should go backward in one of you list while multipling coefs.
It's a kind of convolution

good luck

monkey_king 39 Junior Poster

ok thanks again for the clarification.
So just inling the stuff wont make the program safe,
since it's up to the compiler to decide if it wants to inline it.


about stl,
So they just have a huge class{} with the implemention directly inputtet.

If I do this, should my program the conform with old style
staged compiling.
that is compile everything to object code,
and then link?

monkey_king 39 Junior Poster

you should try compiling each of the files into object code,
something like
g++ -c yourfile.cpp
This should check if your compiler is workings,
not the crosscompilation linker and all that stuff.

To salem.
you can compile headers,
these are called .pch og h.gch
stdafx on visual studio is an example of a precompiled header.

monkey_king 39 Junior Poster

Hi i am the original poster from the other thread referenced to you.

It's very difficult to get decent information,
I have actually been reading a oldstyle book,
to understand this.

If you just want it to work do as stuxyz told me.

On a deeper level,
I think partial specialization is used only for member functions of a templated class.
whereas templated functions are over loaded.


that is
a partial template will extend an already existing template
a function overload will create a completely new.

good luck

monkey_king 39 Junior Poster

Without having looked to much on your code,
I would simply shadow the "numberofpayments" variabel in a greater scope,
and print out the outputline after the main while loop

monkey_king 39 Junior Poster

thanks styxyz!
I haden't grasped, that I needed to
move implemention of my templatede class to a cpp file and add a
'templete class NAME<type>'
and compile object code and link with this file.

this is for sure the compile link model I will be using,
since I understand what's happening!

still
1)
I don't really understand why the inlining these specialized inlined member functions of templetes removes the multi def error.
2)
How the officiel stl libs does this.

But thanks for your very helpfull comments, codeexamples and commandline arguments.

monkey_king 39 Junior Poster

I had a program where I need to read in data,
I ended up tokenizing each line and looping while(!.eof())

monkey_king 39 Junior Poster

Hi thanks for your replies, you have been very helpfull and informative.

But I'm afraid the problem persists.

I've simplefied my files even futher such that it should be simpler to check. ( only1 templated header with implementation in samefile)
Attached in the bottom of this post


@stuxyz
I've tried doing the instantiation of type
template class Foo<float>, in all files throughout the program, and none solves the problem.
That would be TemplatedFoo.h class1.h class1.cpp and ultra.cpp.
And your cutdown example doesn't really cover mine. Mine can compile as far as yours can, but I'm generating object code with another class using my templated class, and then try to link this with a main().
It's the last step that is causing the problem.

@siddhant3s
The problem to which you a refering is not really the same is mine. The poster has the problem of undefined refs, mine is multiple definitions.

I don't think the parashift example covers my problem,
since the returntype of one of the methods of class1 is a Foo<float>,
so that should be instantiated.

As I wrote in my original post the problem disappears magicly with inlining the function specializations. But this seems like a rude hack, with to much object code being compiled.

If I try to do the same with a stl type, there are no problems, does anyone know how they avoid this problem?

thanks …

monkey_king 39 Junior Poster

Hi stuxyz,
I just saw you had replied to my original post,
while I was replying,
So I didn't see it till know.

It's seems very informative and promising you post,
and i will for sure let you know how it goes.

thanks for you reply :)

monkey_king 39 Junior Poster

Thanks for you replies.

sorry there is indeed a #endif in file,
sorry for not copying the file correctly.

The problem arises when I compile a seperat object.
and try to link it with another object file that includes the same (but include guarded header file).

monkey_king 39 Junior Poster

Please help me with this.

I am a newbie at this.

I am supposed to pass a string array to a function. Letter grade Please give me an example on how to do it plz.

thanks in advance.

What do you mean,
an array of strings?
og just a string

have you simple tried

int fun(std::string name);
int fun(*std::string names);
int fun(std::vector<std::string> names);
monkey_king 39 Junior Poster

So it's a minimum spanning tree right?

if so use either kruskall or prim
http://www.boost.org/doc/libs/1_38_0/libs/graph/doc/index.html

But don't bother with the implementation just use boost.

monkey_king 39 Junior Poster

I'm having troubles linking together different classes that uses the same templated class.

I found 2 workarounds, that will make it compile, but
it's not really a solution.
1. don't do a staged compiling with linking object code
2. inlining the spezialized member functions.

I'm looking for a solution that will make my templated class work just as nicely as the official stl's.


TemplatedFoo.h is an include guarded class definition that includes the implemention directly from TemplatedFoo.impl.

class1.cpp includes TemplatedFoo.h, and i can compile object code with 'g++ -c class1.cpp',
But when i try to link it with other stuff I get these kind of errors.

g++ ultra.cpp class1.o
class1.o: In function `Foo<float>::Foo(float)':
class1.cpp:(.text+0x128): multiple definition of `Foo<float>::Foo(float)'
/tmp/ccE4UJX8.o:ultra.cpp:(.text+0x6c): first defined here
class1.o: In function `Foo<float>::Foo(float)':
class1.cpp:(.text+0x17e): multiple definition of `Foo<float>::Foo(float)'
/tmp/ccE4UJX8.o:ultra.cpp:(.text+0x98): first defined here
class1.o: In function `Foo<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Foo(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
class1.cpp:(.text+0x1aa): multiple definition of `Foo<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Foo(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccE4UJX8.o:ultra.cpp:(.text+0x1ae): first defined here
class1.o: In function `Foo<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Foo(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
class1.cpp:(.text+0x1fa): multiple definition of `Foo<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Foo(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccE4UJX8.o:ultra.cpp:(.text+0x1fe): first defined here
collect2: ld returned 1 exit status

This is the code.

//TemplatedFoo.h
#ifndef TEMPLATED_FOO_H
#define TEMPLATED_FOO_H

template <typename T>
class Foo
{
public:
  Foo(const T value);
  T getValue() const;
    
private:
  T m_value;
};
#include "TemplatedFoo.impl"
//TemplatedFoo.impl
template <>
Foo<float>::Foo(const float value ) {
std::cout << "I do floats:\n";
  m_value …
monkey_king 39 Junior Poster

Hey,
does anyone know if it possible to have something like

#DEFINE NUM_DEC 4

printf("This is your value:%.NUM_DECf\n");

thanks in advance

monkey_king 39 Junior Poster

Hi,
I've written a program for statistic analysis that can be found here
http://staff.pubhealth.ku.dk/~ande/web/software/relate.html

I want to make it portable and the program works on unix with the following compilers
sun, intel and gcc
It also compiles on windows with mingw and the intel compiler.

I've installed ms studio,
but I have yet to find a sort of Makefile way of installing the program.

Can anyone give me an advice on how to make the compilation step easier for the end windows user.

Do I have to install cmake to generate an msproject/workspace or whatever it's called?
It seems like an overkill just for 8 compilations and 2 times linking.

I mostly an unix user, and most of the window stuff just puzzles me.

Just the commandpromt. I cant even change the width of this.
So when I type in commands with long dir names it spans over multiple lines.

but also the object files aren't called .o under windows,
so what I do is to compile manually with something like

cl -c myfile.cpp

Has anyone any experience with this?

thanks in advance

btw this is my makefile

CC=g++
C=gcc



#this is the flags used for dev.
flags=-ggdb -Wall -pedantic -ansi 

#flags= -O2 -pg

#this is the flag used for prod.
#flags=-O3

all : relateHMM.o relateHMMRun HMMtest


bfgs.o : bfgs.c bfgs.h types.h
	$(C) -c -fPIC -std=gnu99 -O2  bfgs.c 

alloc.o : …
monkey_king 39 Junior Poster

Thanks makes sense,
And I guess it's the same with the profiling?

monkey_king 39 Junior Poster

Hi,
I was just wondering if adding debug symbols to my program,
will slow down it's performance.

I'm running linux.

thanks in advance

monkey_king 39 Junior Poster

Hi I'm trying to convert a linux/gcc program to windows/cl

But I'm having some problems understanding the inner workings of the ide called microsoft visual studio c++ 2008 express.

I've started a "new clr project", and then added the .hpp file,
that contains the header.
But the compiler/f7 says that it cant find the file even though it appers in the solution explorer.
below is a screenshot.

btw I've tried dragging the file from source files to header files,
and still doesn't work.

Do I have to set som strange compiler include even though a file appers the the solution explorer


http://img87.imageshack.us/img87/8973/danilq5.jpg

thanks

monkey_king 39 Junior Poster

I'm using linux.
But thanks.

I have programs that works on textfiles,
these does parsing and conversion.

instead of writing

./myprog afile.txt

I would like to able to do

cat afile.txt|./myprog
monkey_king 39 Junior Poster

Hi,
I was thinking about implementing piping and redirection in some of my programs.

But I'm having difficilties getting started.
Does anyone know were to read up on this area,
or just what to google.

Google only gives my tutorials on basic unix commandline usage.

thanks in advance

monkey_king 39 Junior Poster

I've made a program using gcc as a compiler.

In my loops I've been using a uint type as the type of my loop variable.

But now it seems that some systems doesn't support this type.

Can I make a preprocessor conditional that does a

#define uint to unsigned int

if typename uint doesn't exist

thanks in advance

monkey_king 39 Junior Poster

Hi I managed to nail this one down myself.

Whenever you define an iterator that depends on a templated type,
you have to use keyword typename infront.

like this

template<typename T>
void test(Array<T> ai){
  typedef std::multimap< T, int >  asso;
  asso dict;
  for (int i=0 ; i<5 ; i++){
   dict.insert(make_pair(ai(i),i));
  }
  
  typename std::multimap<T, int>::iterator pos;
  
  for(pos=dict.begin() ; pos!=dict.end() ; pos++)
    cout << pos->first <<" next "<< pos->second <<endl;
}

thanks for your help

monkey_king 39 Junior Poster

Ok,
I was just trying to avoid an information overload for people trying to make sense of my problem,
Attached is the a RMatrix.cpp with contains the header and the implementation,
The file called test.cpp is the error prone codesnippet.

thanks in advance

monkey_king 39 Junior Poster

Sorry for not elaborating.
this is the header for my class

template<typename T>
class Array {
 public:
  /*
    arrayconstructors
    1. copy constructor
    2. read from file constructor without delim
    3. read from file constructor with delim
    4. dimension constructor
    5. data constructor
   */
  
  //iterator support
  // type definitions
  
  typedef T              value_type;
  typedef T*             iterator;
  typedef const T*       const_iterator;
  typedef T&             reference;
  typedef const T&       const_reference;
  typedef std::size_t    size_type;
  typedef std::ptrdiff_t difference_type;
  
        // iterator support
  iterator begin() { return data_; }
  const_iterator begin() const { return data_; }
  iterator end() { return data_+x_; }
  const_iterator end() const { return data_+x_; }
  /*
  typedef T* iterator;
  typedef const T* const_iterator;
  typedef T& reference;
  iterator begin() {return data_;}
  const_iterator begin() const {return data_;}
  iterator end() {return data_+x_;}
  const_iterator end() const {return data_+x_;}
  */


  Array(const Array<T>& var);
  Array(std::string str,std::string delims);
  Array(std::string str) ;
  Array(uint length);
  Array(T* d,uint a);
  ~Array()/// Only tries to free memory if length of Array is bigger than one.
  {if(x_!=0){delete [] data_;}}

  /*
    simple accessors and mutators
   */
  ///Returns the length of the array.
  uint length() const                  { return x_;/// @return Gives the length of
the array.   
  }
  T& operator() (uint r); //inlined 
  T operator() (uint r) const; //inlined

  /*
     These crazy definitions are requried since
     Array<U> cannot access internals of Array<T>
     So methods requiring internals should be friends
     
     This is comparison operators and extractors
   */
  //first declarations
  Array<char> operator& (const Array<T> &f);///< A keeplist using bitwize AND '&'.
  Array<char> operator| (const Array<T> &f);///< A keeplist using bitwize OR …
monkey_king 39 Junior Poster

Whats your error message?

are you using namespace std
otherwise string should be std::string

monkey_king 39 Junior Poster

I wan't use the multimap on my own templated class.
The following is a working example with a builtin type.

int dat[5] = {0,6,3,7,3};
  
 typedef std::multimap< int, int >  asso;
 asso dict;
 for (int i=0;i<5;i++){
   dict.insert(make_pair(dat[i],i));
 }
  
  asso::iterator pos;
  
  for(pos=dict.begin(); pos!=dict.end() ;pos++)
    cout << pos->first <<" next "<< pos->second <<endl;

This is my non-working code with my own templated type.

template<typename T>
void test(Array<T> ai){
  typedef std::multimap< Array<T>, int >  asso;
  asso dict;
  for (int i=0 ; i<5 ; i++){
   dict.insert(make_pair(ai(i),i));
  }
  
  asso::iterator pos;
  
  for(pos=dict.begin() ; pos!=dict.end() ; pos++)
    cout << pos->first <<" next "<< pos->second <<endl;
}

Where my own templated type is called 'Array<T>'.

I think the problem is related to the std::multimap using iterators,
and I've never really understood iterators, and even less how to write my own iterators for my own templates.
This is my iterator definitions and implementation in my class

typedef T              value_type;
  typedef T*             iterator;
  typedef const T*       const_iterator;
  typedef T&             reference;
  typedef const T&       const_reference;
  typedef std::size_t    size_type;
  typedef std::ptrdiff_t difference_type;
  
        // iterator support
  iterator begin() { return data_; }
  const_iterator begin() const { return data_; }
  iterator end() { return data_+x_; }
  const_iterator end() const { return data_+x_; }

thanks in advance

monkey_king 39 Junior Poster

Thanks,
it's working now thanks to your help.
The syntax for operator overloading is quite strange.

I'm doing a template matrix class,
And I was looking into using valarrays as rows.
But In the end I decided to do everything by myself.

Now is a good as time ever to learn this template stuff.

But thanks.

monkey_king 39 Junior Poster

As I wrote, I've already implemented the "myclass + primitiveVal" overload.
And I don't need to use a constructor for this.

the RHS in my code is

//overloads '+' with a single value
template<class T>
Array<T>  Array<T>::operator+( const float f ){
  Array<T> tmp(x_);
  for(uint i=0;i<x_;i++) 
    tmp.data_[i] = data_[i] +f;
  return tmp;

And the LHS is a simple assign,

but can you elaborate on the signature for the other one?

monkey_king 39 Junior Poster

I've been overloading some methods for a template class.
This means I can write my code in this way.

myClass var3=var2+4;

Is it possible to do it the otherway around like

myClass var3=4+var2;

I know that the end result will be the same,
I was just wondering.

thanks

monkey_king 39 Junior Poster

I can't compile, I'm given this crazy compile error.

matrix.cpp: In function ‘int main()’:
matrix.cpp:464: error: no match for ‘operator<<’ in ‘std::cout <<
Array<T>::operator+(const Array<T>&) [with T = int](((const Array<int>&)((const
Array<int>*)(& a2))))’
/usr/include/c++/4.2/ostream:112: note: candidates are: std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT,
_Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.2/ostream:121: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT,
_Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.2/ostream:131: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base&
(*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:169: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT =
char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:173: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with
_CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:177: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char,
_Traits = std::char_traits<char>]
/usr/include/c++/4.2/bits/ostream.tcc:92: note:                
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(short int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.2/ostream:184: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with
_CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/bits/ostream.tcc:106: note:                
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:195: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT
= char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:204: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with
_CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:208: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int)
[with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:213: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT =
char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:217: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT =
char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2/ostream:225: note:                 std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT
= char, _Traits = std::char_traits<char>] …
monkey_king 39 Junior Poster

Hi, I've been implementing my own array class,
and I've finally started to use it.
I do have one question,

Array<int> ary1(3);ary1(0)=2;ary1(1)=4;ary1(2)=7; //just an array
  Array<int> ary2(3);ary2(0)=1;ary2(1)=2;ary2(2)=6; //just an array
  cout << ary1;
  cout << ary2;
  Array<int> ary3 = ary1+ary2;
  cout << ary3;
//everything till this point works

  //is this possible
  cout << (Array<int>) ary1+ary2;
  return 0;

Do I need to assign a temp Array<int> just for outputting.

This is the interface for my code

//member function of Array<T>
std::ostream& dump(std::ostream &o=(std::cout),char sep=' ');

//free function
std::ostream& operator<<(std::ostream &o,Array<T> &a){
  return a.dump(o,' ');
}

thanks in advance

monkey_king 39 Junior Poster

Thanks Narue,
it works perfectly now.

monkey_king 39 Junior Poster

Hi I'm doing a templated matrix class,
and I'd like to be able to pass a function pointer to a function that uses the rows.

template<typename T>
T sum(Array<T> &a){ // a simple function that return the sum of an Array
  T res=0;
  for(int i=0;i<a.length();i++)
    res+=a(i);
  return res;
}

template<typename T>
Array<T> getrow(Matrix<T> &m,int r){// a simple function that return a row from a matrix
  Array<T> retAry(m.y());
  for(int i=0;i<m.y();i++)
    retAry(i) = m(r,i);
  return retAry;
}


//this is the function I'd like to implement
template<typename T>
Array<T> apply(Matrix<T> &m,T (*fun)(Array<T>)){
  Array<T> retA(m.x());
  for (int i=0;i<m.x();i++){
    Array<T> r = getrow(m,i);
    retA(i) = (*fun)(r);
  } 
  return  retA;
}

//same as above but with out the function pointer
template<typename T>
Array<T> applyThisWorks(Matrix<T> &m){
  Array<T> retA(m.x());
  for (int i=0;i<m.x();i++){
    Array<T> r = getrow(m,i);
    retA(i) = sum(r);
  } 
  return  retA;
}


int main(){
  Matrix<int> m(3,5);
  fillIt(m);
  m.print();
  Array<int> var = applyThisWorks(m);
  //Array<int> var = apply(m,&sum);//how do I call the function?
  
  cout <<"this is the rowwise sum:"<<var;
}

My code is of cause a lot more, I can post it,
if you need it.
Everything is working, and when I use the nonfunction pointer I get the desired output.

dim is: (3,5)
0 1 2 3 4 
5 6 7 8 9 
10 11 12 13 14 
this is the rowwise sum:10 35 60

But If I try to compile the desired general version i get this error

matrix.cpp: In function ‘int main()’:
matrix.cpp:230: error: no matching function for call to …
monkey_king 39 Junior Poster

Thanks for your help.
I gues I just go with return double always.
I can see from vijayan121 response,
that the solution is not quite as straight forward as I had hoped.

thanks again.

monkey_king 39 Junior Poster

>
>They differ by the array calling the operator.
As far as the compiler is concerned, Array<int> is simply a more specific version of Array<T> and will try to match the declaration using that type. Since you said originally that operator/ returns an Array<T>, then when T is int, the compiler expects to see a return value of Array<int>. That's all there is to it.

>But it doesn't solve the general problem.
In my opinion the general problem is a design flaw on your part. You're trying to get behavior that, while I can see your reasoning and can admit to its convenience, is unconventional and ultimately breeds confusion and horrible workarounds in the cases where users don't want a conversion from Array<int> to Array<float> during division.

Hmm, I'm quite new into this template,
and you are properly right, it's a bad design, and I guess the compiler is even more confused than I am.

But I still find it ackward, that there are no possibility to do it.
I can handle special cases when, I have different INPARS as long as the method is void.
But it's not possible to do it when you have a return type.

I do understand that there is no way around this, the compiler is always right.
But I don't see the rationale

>
>but exception handling and catching, I always do when the program actually works.
Then your definition of …

monkey_king 39 Junior Poster

>But if I use a array<int>, i don't want it to return
>a array<int>, it should return a array<float>.
Overloads can't differ only by return type.....

They don't differ by return type,
They differ by the array calling the operator.
that is

Array<float> fArray(10);
fArray returnArray = fArray / 2.0;
vs
Array<int> iArray(10);
iArray returnArray = iArray / 2.0;

What you are suggesting is of cause a nice and easy work around.
But it doesn't solve the general problem.
It there is one, that is.

>
>puts("ohh my god, divide by zero thats stupid, will exit");
>exit(0);
Terminating the program from a library class is equally stupid. I'd recommend throwing an exception instead. That way the application using your Array class can attempt to recover from an eminently recoverable error. Failure to support decent error handling results in nobody wanting to use your classes.

I know I know,
but exception handling and catching, I always do when the program actually works.

Thanks for the reply

monkey_king 39 Junior Poster

I'm doing my own array template version of an array.
I'm having some problems overloading a method.
I'm doing a specifik version of a "print()" if the type of array is <char>,(i'm saving bools as char).
this works without problems. I just needed to add a templete<> infront of my scpeciales "print()". This is shown at line 32-47.


Now I need to do an array version of division.
The general case is an array of floats, this will work without problem
So given normal Array with floats, my general version line 51-62 works.

But if I use a array<int>, i don't want it to return a array<int>, it should return a array<float>. (like float division)

But I'm having problems with the defintion.
Can someone give me a solution, thanks.

#include <iostream>

template<typename T>
class Array {
 public:
  Array() {puts("empty constructor");data_=NULL;x_=0;}
  Array(int length):x_(length),data_(new T[length]){}
  void init(int length) {x_=length;data_=new T[length]; }
  Array(const Array<T>& var);
  ~Array(){delete [] data_;}
  int length() const                  { return x_;     }
  T& operator() (uint r); 
  T operator() (uint r) const; 

  void print(int x=0, char sep=' ');
  void fillUp(T var) {for(int i=0;i<x_;i++) data_[i]=var;}
  void plus(const Array<T>&);
  Array<char> operator< (const float &f);
  Array<char> operator> (const float &f);
  Array<char> operator== (const float &f);
  int numTrues() const {return numOnes_;}

  Array<T>& operator= (const Array<T>&);
  Array<T> operator+ (const Array<T>&);
  Array<T> operator/ (const float &other);

private:
  int x_;
  T*  data_;
  int numOnes_;
};

template<typename T>
void Array<T>::print(int x,char sep){
  printf("printing array with dim=%d\n",x_);
    for(int …
monkey_king 39 Junior Poster

You can't obtain a reference to bit-field in C++:

A non-const reference shall not be bound to a bit-field (8.5.3).
Note: if the initializer for a reference of type const T& is an lvalue that refers to a bit-field, the reference is bound to a temporary initialized to hold the value of the bit-field; the reference is not bound to the bit-field directly.

For the sake of documentation,
can you tell me where got this info.
(for my boss)

thanks

monkey_king 39 Junior Poster

Thanks, I thought it was possible to avoid instantiation,
untill runtime, and then choose.
But I'll define my aClass and aTemplate with a pointer then.

thanks again.

monkey_king 39 Junior Poster

Hi, I've been programming a lot of c code,
and now I'm trying to set my mind into the c++ way of doing things.
so I'm trying to avoid pointers, and use refs instead.
As far as I understand pointers are to be avoided using stl's for instance.


I have 2 matrix classes that I want to contain in another class.
But at runtime I only want one of the matrixobjects allocated and instantiated.

In good old c, I would have made a pointer to the object,
and then only allocated the one I wanted.

But It seems that you need to instantiate everything in a class.

Or am I trying to do this in a odd way?

I think the problem is in class "aCollector" at line 20-27

#include <iostream>

class aClass{
 public:
  float f1;
  float f2;
  int *array;
  aClass(float var1, float var2){printf("norm class construct\n");f1=var1;f2=var2;}
};

template <typename T>
class aTemplate{
  float f1;
  float f2;
  T *array;
  aTemplate(float var1, float var2){printf("template class constructor\n");f1=var1;f2=var2;}
};


class aCollector {
public:
  int a;
  int b;
  aCollector(int chooseType,int f, int g): (chooseType==0)?myTemplate(f,g):myobj(fg){}
  aTemplate<int> myTemplate;
  aClass myobj;
};

int myFun(aStruct as){
  return 0;
}

int main(){
  aCollector acol(2,3);
  return 0;
}

thanks in advance

monkey_king 39 Junior Poster

Hey everyone, still learning c++ I had a question.

How do I create an array that will hold strings of text.

such as:

??? map[10];
map[1]= "text";
map[2] = "text2";
map[10] = "text3";
cout<< map[1] "\n";

help would be great
ink

You can do it in many different ways,
but I think c++ fanatics recommends using stl vector,
theres a little example here.

http://www.codersource.net/c++_vector_stl.html .

But i would just do

#include <iostream>
#include <string>

using namespace std;
int main(){
  string array[10];

  array[0] = "element1";
  array[1] = "element2";

  cout << array[0] <<endl;
}

Hope it helps

monkey_king 39 Junior Poster

Thank you,
You've been very helpfull.

monkey_king 39 Junior Poster

I depends on the version of mpi, the scheduler and most of all how the admin has setup the system.

If the admin is you,
then you might have to setup a small script.

I did the following when I installed mpi.

my version of mpi used a list of hosts in some dot file int my home lib.

So I read a list of available hosts from the scheduler and put these in the dot file.

good luck

monkey_king 39 Junior Poster

Hi, I'm doing a low-mem implementation of a matrix, than can contain values from 0-4, this is fine for my program.
I input 4 values (each 2bit) into 1 char(8bit). I'm using bitpatterns and shifting to extract values. But this is of cause hidden from the user.

I've done everything but I'm having problem overloading the "()" operator.
I can input values, and get values, using ordinary function like

int a = obj.get(a,b); //returns value at row a, column b
obj.set(a,b,c); //insert value c at row a, column b.

But since this is c++, I want a nice consistent interface with my other matrix template class.

This means I would like to implement overloading of the "()" in such a way that

int d = obj(a,b); //set value d to the value at row a, col b
obj(a,b) = c;     // set value at row a, col b to c

Below is the interface for my class, along with my try at implementing the overloaded operators.

class snp{
private:
  unsigned char **alloc(int x, int y);
  unsigned char **m_data;
  size_t x; //number of rows.
  size_t y; // this is the number of hole used chars
  size_t offset; //offset
public:
  snp(int a,int b);
  ~snp()          {delete [] *m_data; delete [] m_data;}
  int get(int a, int b);
  void set(int a, int b, int c);
  int& operator() (uint r, uint c);
  int operator() (uint r, uint c) const;
};
//below is implementation
int& snp::operator() (uint r, uint c){ …
monkey_king 39 Junior Poster

Thanks very informative.

But I had another question.
The use of unsigned vs signed variabels.
I never use negative values in, say, control variabels in loops.
So should I begin to use uint?
Alot of people are using size_t to denote non negative size's in datastructures.

Is this a good thing to adopt.

Thanks again for you replys.

monkey_king 39 Junior Poster

I don't really understand what your doing here,
this has nothing to do with linked lists.

you need to do use a struct like

typedef struct lnode
{
int data;
struct lnode *next;
} listnode_t;

monkey_king 39 Junior Poster

Can you elaborate on your problem.
Given the subject of your post,
I thought you had problems opening a file in your code.
But it seems there are no file input/output.

Can you compile the code, and if you can't compile what is the error message.

monkey_king 39 Junior Poster

Hi I have a some general questions, some are c others are c++ related.
So I'll just post them here in the c++ forum.
These question only relates to speed not so much design issues.

So far most of my programming has just been about making it work.
That is getting the program done fast and correct.
I haven't been concerned about speed and portability.

  1. I know it's good programming practice to make accessor's const.
    But does it impact speed.
    An example:
    If I have a huge matrix class, which I will only read from, after I have instantiated the object.
    Will I get any speed benifits from making the the accessors const. (making it nonmutable)?
  2. Will I get any performance gains by using static keywords, where ever it's possible
  3. Will I get any performance gains by using const keywords, where ever it's possible
  4. If I made a uml diagram of my programs, they look relative alike. I never use the "smart" stuff, like protected and virtual function. Should I start looking into this. Or is it simply something that you use when you have to give a presentation. According to http://people.redhat.com/drepper/dsohowto.pdf the virtual functions are especially to avoid.
  5. scope of variables. I was always told to avoid using global variables, usually after being told never to use gotos. But other than the confusion of variable shadowing, what is so bad about it. Also recycling of temp vars.