0

Source is very primitive and uses Stanford's lib vector.h

#include <iostream>
#include "vector.h"

void countIntRange(Vector<int>& v, int a, int b);

int main(){
    Vector <int> v;
    v.add(28);
    v.add(1);
    v.add(17);
    v.add(4);
    v.add(41);
    v.add(9);
    v.add(59);
    v.add(8);
    v.add(31);
    v.add(30);
    v.add(25);
    int min = 10;
    int max = 30;
    countIntRange(v, min, max);

    return 0;
}

void countIntRange(Vector<int>& v, int a, int b){
    int count = 0;
    for (int i = 0; i < v.size(); i++){
        if (v.get(i) > a && v.get(i) < b){
            count++;
        }
    }
    cout << count;
}

while compiling g++ throws such error:

highlander@linux-f62d:~/Documents/CS106b/libs> g++ ex31.cpp
/tmp/ccfH1i0N.o: In function `Vector<int>::get(int) const':
ex31.cpp:(.text._ZNK6VectorIiE3getEi[_ZNK6VectorIiE3getEi]+0x4b): 
    undefined reference to `error(std::string)'
/tmp/ccfH1i0N.o: In function `Vector<int>::insert(int, int)':
ex31.cpp:(.text._ZN6VectorIiE6insertEii[_ZN6VectorIiE6insertEii]+0x6c): 
    undefined reference to `error(std::string)'
collect2: error: ld returned 1 exit status

This is error message.... What it doesn't like?

Here goes whole vector.h code https://gist.github.com/HighlanderGe/9023734

Frakcool
  • 10,915
  • 9
  • 50
  • 89
Tsiskreli
  • 103
  • 1
  • 11
  • You have to show us the `get` and `insert` function of your `Vector` class – yizzlez Feb 15 '14 at 18:58
  • `undefined reference` is likely a missing library –  Feb 15 '14 at 19:03
  • You either didn't compile the given code into a library that you can link to via `g++` or you didn't put `vector.h` (and all other dependencies) in your compile command. Just a curious question though, why don't you use `std::vector`? –  Feb 15 '14 at 19:27
  • I'm newbee, so please explain better how to compile library ? – Tsiskreli Feb 15 '14 at 19:57

2 Answers2

1

The way to solve it depends on how you are using the Stanford cslib library:

  • If you compiled it into a library, say StanfordCSLib, you need to add -lStanfordCS to your g++ command line
  • If you want to use library sources directly, you need to add StanfordCS/*.cpp to the list of files that you give g++.

The first approach is better, because it avoids multiple recompiles. The second approach may be more expedient if you need to get a single set of sources to work.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you, but, in case of -lStanfordCS I must install it somehow so that g++ will know what I mean but now I just have directory with files... – Tsiskreli Feb 15 '14 at 19:14
0

Here is how i get this working:

  1. Create a header file named vector.h and copy paste all the file contents in it.
  2. Comment out lines for the files foreach.h and strlib.h in vector.h.
  3. Create a custom header file and add an include of this file top of vector.h file with contents of following:
#include <iostream>
#include <string>

void error(std::string msg)
{
  std::cout << msg << std::endl;
}

4.Compile source file with new header files(vector.h and custom_header.h)

Fredrick Gauss
  • 5,126
  • 1
  • 28
  • 44