abhimanipal 91 Master Poster

They do. The issue was that my constructor were defined in the .cpp file which was not getting included in the child class. Apparently netbeans does not handle these things automatically (or I am doing some thing wrong)

abhimanipal 91 Master Poster

@Mike
Here are the definitions for the functions

template<class V, class I> Array<V,I>::Array(){

}

template<class V, class I> Array<V,I>::Array(I size){
    this->myStore = vector<V>(size);
}

template<class V, class I> Array<V,I>::Array(I size, V value){
    this->myStore = vector<V>(size);
    for(int i =0;i<size;i++)
        this->myStore[i] =value ;    
}

template<class V, class I> Array<V,I>::~Array(){

}
abhimanipal 91 Master Poster

Hi Guys, I am trying to teach myself templates and I keep getting lost in all these compilation errors. Here is the latest

// This is the interface for the base class
template<class V, class I =int>
class Array{

    private:
        vector<V> myStore;

    public:
        Array();
        Array(I size);
        Array(I size, V value);
        ~Array();

        int size();
        const V& element(I index);
        V& operator[] (I index);

        void print();
};

// This is the interface for the derived class
template<class V, class I=int>
class NumericArray: public Array<V,I>{

    public:
        NumericArray();
        NumericArray(I size){}
        NumericArray(int size, double value){}
        ~NumericArray(){}

//        void print();
};

This is my implementation for the constructor
template<class V, class I> NumericArray<V,I>::NumericArray(I size):Array<V,I>(size){

}
But this keeps giving me a compiler error. undefined reference to `Array<double, int>::Array(int)'. 

What am I doing wrong here? Any help would be much appreciated.

abhimanipal 91 Master Poster

@decepticon thanks so much for the answer. One more question can you elaborate on the need for returning a const reference?

abhimanipal 91 Master Poster

Hi Guys, I am trying to teach myself templates in C++.
My current assignment is to create a matrix class by using vector of vectors. I have got most of it done but my code is crashing when I try to overload the random access operator in my matrix class. Here are the relevant code snippets

// This is the array class and I think this works fine
    template<class V, class I =int>
    class Array{

        private:
            vector<V> myStore;

        public:
            Array();
            Array(int size);
            Array(int size, V value);
            ~Array();

            int size();
            const V& element(I index);
            V& operator[] (I index);
    };

// This is the function definition for the Array random access operator    


    template<class V, class I> V& Array<V,I>::operator [](I index){
        return this->myStore[index];
    }



// This is the matrix class 


    template<class V, class I = int>
    class Matrix{

        private:
            vector<Array<V,I> >myStore;

        public:
            Matrix();
            Matrix(int rows, int columns);
            Matrix(int rows, int columns, V value);
            ~Matrix();

            int rows();
            int columns();

            V& operator() (int row, int column);
    };

// This is the function definition for the Matrix random access operator. 


    template<class V, class I> V& Matrix<V,I>::operator ()(int row, int column){
        Array<V,I> myArray = this->myStore[row];    
        return myArray[column];
    }

My code does not crash when I return by value from the above function. What am I doing wrong. Any help is much appreciated

abhimanipal 91 Master Poster

Hi Folks,

Here is another noob question. I am trying to write a simple predicate for the find_if function in STL. Here is my code

#include<iostream>
#include<vector>
#include<algorithm>
#include<iterator>

using namespace std;

class InRange{
        const int& low;
        const int& high;
public:
        InRange(const int& l, const int& h): low(l), high(h){}
        bool operator() (const int& myValue){return (myValue > low) && (myValue < high);}
};

int main(){
        vector<int> myVector(8);
        myVector.push_back(10);
        myVector.push_back(15);
        myVector.push_back(20);
        myVector.push_back(25);
        myVector.push_back(30);

        vector<int>::iterator myIterator;
        copy(myVector.begin(), myVector.end(), ostream_iterator<int> (cout," "));
        cout<<endl;

        myIterator = find(myVector.begin(), myVector.end(), InRange(10,20));
        cout<<"Found "<<*myIterator<<endl;
        cout<<endl;
}

However I am making some obvious mistake as the code just does not compile. Here is the compiler error. Any ideas of what I am doing wrong. Sorry for this huge error message but for some reason all the compiler messages that I get for STL related errors tend to be very verbose with actually telling me what the error is

In file included from /usr/include/c++/4.4/algorithm:62,
                 from test.cpp:3:
/usr/include/c++/4.4/bits/stl_algo.h: In function ‘_RandomAccessIterator std::__find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _Tp = InRange]’:
/usr/include/c++/4.4/bits/stl_algo.h:4224:   instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _Tp = InRange]’
test.cpp:31:   instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:174: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]() == __val’
/usr/include/c++/4.4/bits/stl_algo.h:4224:   instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _Tp = InRange]’
test.cpp:31:   instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:178: error: no match for ‘operator==’ in …
abhimanipal 91 Master Poster

Hi everybody,

Please excuse the noob question as I am new to STL in C++. I am trying to write a very basic program which uses iota. However my compiler is complaining that iota does not exist in algorithm. Some other forums suggested including <backward/algo.h> as iota could have been depreciated. However my code still does not compile. Can some one take a look?

#include<iostream>
#include<vector>
#include<algorithm>
#include<iterator>
#include<backward/algo.h>

using namespace std;

int main(){
        vector<int> myVector;
        iota(myVector.begin(), myVector.end(),100);
        copy(myVector.begin(), myVector.end(), ostream_iterator<int>(cout," "));
        cout<<endl;
}

I am using g++ on Ubuntu and my compiler version is 4.4.3

abhimanipal 91 Master Poster

Hi Stu,

Thanks a lot for your detailed posts, I appreciate. I used a some what different approach to solve my problem.

template <class RandomAccessIterator>
  void display(RandomAccessIterator myStartIterator, RandomAccessIterator myEndIterator)

I am sure what you said probably makes sense as well, but I found this to be an easier approach. I am more of a C guy and I am trying to get my feet wet in STL.

abhimanipal 91 Master Poster

display<int>(myBeginIterator,myEndIterator);

Wouldn't this defeat the point of using templates? Or in other words why do we have the int in the function definition?

abhimanipal 91 Master Poster

Hello everybody,

I am trying to teach my stl and after writing some basic programs I have hit a road block. I have an iterator which I am trying to pass to a function template

Here is my function template

template <class T>
void display(typename vector<T>::iterator start, typename vector<T>::iterator end){
        cout<<"My display "<<*start<<endl;
}

Here is the call to this function

    vector<int>::iterator myBeginIterator;
    vector<int>::iterator myEndIterator;

    myBeginIterator = myVector.begin();
    myEndIterator   = myVector.end();
    display(myBeginIterator,myEndIterator);

Howevert this code refuses to compile. The compilation error is

error: no matching function for call to ‘display(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&)

I am pretty sure this is a noob mistake. I am more of a C guy as opposed to a C++ guy. Can some one take a look at the code snippet and tell me if there is an obvious bug

abhimanipal 91 Master Poster

I prefer to pass arrays like this

void name(char *game, int length)
{
//your void function here
}

int main()
{
char game[4] = {'S', 'R', 'D'};

name(game,4)

return 0;
}
abhimanipal 91 Master Poster

Put a start time and end time before the start and end of the function. You should start seeing differences in speed of execution after the number of nodes in the tree cross a certain threshold.

abhimanipal 91 Master Poster

Even after you use fgets you will still have to use some if elses to check if the input violates the range restrictions

abhimanipal 91 Master Poster

Can you add some log statements to the code to narrow down the location where the seg fault appears?

abhimanipal 91 Master Poster

What if the text file contains more than 8 characters before a \n is present? The program will try to store the 9 char at cmd[8] . This will lead to a crash

abhimanipal 91 Master Poster

Also your array contains 4 chars (A,B,C,D). Why are you running the loop from 0 to 6 ?

abhimanipal 91 Master Poster

I am not a 100% sure on this but I believe pointers work with logical memory.
When a C program is compiled, the compiler works in such a manner that the program can be placed any where in the physical memory.

I dont think that you can access a physical memory location from within the program

abhimanipal 91 Master Poster

Print the value of coming in from ch

abhimanipal 91 Master Poster

Please post the complete problem statement .... I have no clue what was written in the "other guys" post and have no desire to go hunting for it

abhimanipal 91 Master Poster

I am sorry but I could not follow your problem statement. Can you please re word your problem statement in a more simpler manner

abhimanipal 91 Master Poster

On line 44 I think both the operands have to be of data type int. I think num4 is of type float

abhimanipal 91 Master Poster

How about a small description of the problem for the people who do not have the K & R text book ????

From the code you have written I guess you are trying to count the number of spaces / tabs / new line characters in a sentence ?
If yes then you have to store all the spaces at 1 particular index in the array lword . Similarly all the tabs have to be stored at 1 particular index

abhimanipal 91 Master Poster

What part of file handling is confusing ?

There are 4 basic operations
1. Read --> Read the contents of a file . Use fread for this
2. Write --> Write to a file . Open the file is write mode and then use fwrite for this.
3. Append --> Append to end of file. Open the file in append mode and then use fwrite
4. seek --> When you use fopen to open a file the file pointer usually points to the start of the file. But if you want to update the contents of a file you have to move the file pointer away from the start. Use fseek for this

Try googling for these functions . You should find lots of sample code / tutorials

abhimanipal 91 Master Poster

1 is kind of possible .... But it is messy
Check out this piece of code

#include<stdio.h>
#include<stdlib.h>

struct student
{
        void *subjects[15];

};

int main() {

        struct student s1;
        int numberOfSubjects;
        int i =0;

        printf("How main subject does the student have \n");
        scanf("%d",&numberOfSubjects);

        for(i=0;i< numberOfSubjects;i++) {
                s1.subjects[i] = (int *)malloc(sizeof(int) *1);
        }

        printf("Now enter the marks\n");

        for(i=0;i<numberOfSubjects;i++){
                printf("Subject %d \n",i);
                scanf("%d",(int *)s1.subjects[i]);
        }

        printf("****************************");

        printf("The entered marks are \n");

        for(i=0;i<numberOfSubjects;i++){
                printf("Subject number:%d -----%d\n",i,*(int *)s1.subjects[i]);
        }

        printf("\n");

        return 0;
}
abhimanipal 91 Master Poster

Just to clarify the control is going inside the if block, the rewind is working properly but the assignment operation is failing ?

If there is an issue with rewind ... try fseek

abhimanipal 91 Master Poster

In the example text that you posted are all chars 16 bit ?

Also is it necessary to convert it to ASCII or can you convert it to unicode ? This link explains how to read unicode chars from a text file

abhimanipal 91 Master Poster

Where is the value for no_of_msg_records being set ?

Are you sure no_of_msg_records contains the correct value before the if statement ?

abhimanipal 91 Master Poster

You are incrementing the x value only inside the if block ... I think you need to increment the x irrespective of the fact if the person is a smoker or a non smoker

abhimanipal 91 Master Poster

Did you go through the link that I posted ?

abhimanipal 91 Master Poster

Check out the sourceforge web site ... There you will see many operating systems in various partial stages of development ... Might make more sense / provide more learning to work on a partial os rather than start some thing totally new

abhimanipal 91 Master Poster

Basically this problem is identifying "strings of interest" log / sin / cos / (x)
You use the strtok function to identify the start and end of these kind of strings

abhimanipal 91 Master Poster

I second the books mentioned by yashsaxena . Both of the books are top notch

abhimanipal 91 Master Poster

@Narue

How does the method described by you handle the case when the binary tree is not complete ie. Some of the intermediate nodes could have 0 (or 1) children

abhimanipal 91 Master Poster

Quick question ... Is you problem that you cannot read a 16 bit character or is the problem that you cannot use the toAscii function on a 16 bit character ?

I think you might find this link useful to read in 16 bit characters ....

I do not think you can use ASCII for characters from a foreign language .... You will have to use Unicode for that

abhimanipal 91 Master Poster

You are repeating the same mistake on line 31 as well

Avoid using similar sounding names for variables .grade/ grades .... are very similar. Invariably you will use grade when you mean grades and the program will crash

abhimanipal 91 Master Poster

What is the exact issue that you are facing ?

Other things that may help you
1. Dont use feof . Read this to know why .
2. Next time please use indentation when you write code / paste code. Its easier for us to read your code

abhimanipal 91 Master Poster

@zaraki

Your loop is missing a condition. What if the node to be deleted is not present in the linked list ?
This problem is usually solved by returning from the function when temp->link == NULL

abhimanipal 91 Master Poster

What is the exact issue that you are facing ?

FYI if you use google you will find lots of examples of linked list code all over internet

abhimanipal 91 Master Poster

The for loop on line 50 can be reduced to

for(index = 0;index <i;index++){
       // Your printing stuff here
}
abhimanipal 91 Master Poster

In line 31 .... The way you are using the scanf is wrong

char arr[10] ={};
scanf("%s",arr);                 // This is the correct way of using scanf

FYI scanf should be avoided as much as possible as it has many gotcha's. Its better to shift to fgets

abhimanipal 91 Master Poster

Does the load on each process have to be the same ? One solution could be to give one job to each process and a distribute the other 4 jobs to random 4 processes ...

abhimanipal 91 Master Poster

You have a hard coded for loop which will run 18 times.
What does it print the rest of time ?

abhimanipal 91 Master Poster

Can you explain the purpose of each argument in the movePlayer function ?
Also what is the function getTitle supposed to return ?

abhimanipal 91 Master Poster

Dude no one is going to go through a 400 line code to find an error. I suggest you to post a smaller piece of code....
Just a random thought I think your issue could be a memory violation.

abhimanipal 91 Master Poster

You do not need to use the stl library to calculate the min / max . Try writing the code on your own .. Let me give you a head start

For Min.

1. Create a temp variable and give it a value of 30,000 [or any such large value].
2. Iterate through your array. At each point compare the value in the temp variable in the array.
3. If the value in the temp variable is less than the value in the array then do nothing. Else store the value in the array in the temp variable.

I hope you get the idea

abhimanipal 91 Master Poster

Where are the printf statements in the code snippet that you have posted ?
What does read Pattern data do ?

abhimanipal 91 Master Poster

I believe so ... The other computer has to be configured to accept incoming connections on that port

abhimanipal 91 Master Poster

The connect system call can be used to connect to a network interface ... (Assuming you know the correct IP address and port number)

abhimanipal 91 Master Poster

Ok .... What is the issue ?

abhimanipal 91 Master Poster

Ok ... I get it now ...

Whats the problem now ?