Vector bug

I have this assignment that asks me to separate a program into 5 files. The program runs fine as a whole, but when I separated them, I keep getting these two bugs that I can't fix:

#1. In file included from critFarmTest.cpp:1:

./farm.h:15:5: error: no template named 'vector'

vector m_Critters; vector m_Critters;

#2. In file included from farmImp.cpp:1:

./farm.h:15:5 error: no template named 'vector'

vector m_Critters; vector m_Critters;


Here are the files:

critFarmTest.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "farm.h"

#include "critter.h"

int main()

{

  Critter crit("Poochie");

  cout << "My critter's name is " << crit.GetName() << endl;

  cout << "\nCreating critter farm.\n";
 
  Farm myFarm(3);

  cout << "\nAdding three critters to the farm.\n";

  myFarm.Add(Critter("Moe"));

  myFarm.Add(Critter("Larry"));

  myFarm.Add(Critter("Curly"));

  cout << "\nCalling Roll...\n";

  myFarm.RollCall();

return 0;

}

critter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

#include <string>

#include <vector>

using namespace std;

class Critter

{

public:

  Critter(const string& name = "");

  string GetName() const;

private:

  string m_Name;

};

critterImp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "critter.h"

Critter::Critter(const string& name):

  m_Name(name)

{}

inline string Critter::GetName() const

{

  return m_Name;

}

farm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

#include <string>

using namespace std;

class Critter;

class Farm

{

public:

  Farm(int spaces = 1);

  void Add(const Critter& aCritter);

  void RollCall() const;

private:

  vector<Critter> m_Critters;

};

farmImp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "farm.h"

#include "critter.h"

Farm::Farm(int spaces)

{

  m_Critters.reserve(spaces);

}

  void Farm::Add(const Critter& aCritter)

{

m_Critters.push_back(aCritter);

}

  void Farm::RollCall() const

{

  for (vector::const_iterator iter = m_Critters.begin();

   iter != m_Critters.end();

  ++iter)

  {

    cout << iter->GetName() << " here.\n";

  }
}

Last edited on
vector m_Critters;
vectors need to be vectors of a particular type. e.g. a vector of ints (vector<int>) or a vector<Critter>, which is probably what you want.
@Ganado
There is a vector<Critter> under the class Farm.

@nuderobmonkey
Ok, I will edit my post and I have included vector in farm.h but I ended up getting this error:
/tmp/critFarmTest-368056.o: In function `main':
critFarmTest.cpp:(.text+0x8d): undefined reference to `Critter::GetName[abi:cxx11]() const'
/tmp/farmImp-b2c6e5.o: In function `Farm::RollCall() const':
farmImp.cpp:(.text+0xd9): undefined reference to `Critter::GetName[abi:cxx11]()const'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
compiler exit status 1
I quoted your first post directly, it shows "vector m_Critters", not "vector<Critter> m_Critters".
Sorry, you are right. I don't know why it came out like that, but "vector<Critter> m_Critters" is in my program.
PLEASE learn to use code tags, it makes reading and commenting on code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/

http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add them.
What are you using as a code editor and how are you pasting into the forum? I also can't see what you posted between other < > lines, like your #includes.

If it's saying that vector isn't a template, then that probably means that you don't have #include <vector> in a place that it needs to be. But I can't tell because how ever you're copypasting is getting messed up.
Last edited on
Thanks! The code should be there now.
Add #include <vector> to farm.h
I am using repl.it and I just copied and pasted it from another forum that I had posted my question on. I have used #include <vector> in farm.h, but I am getting this bug:

/tmp/critFarmTest-368056.o: In function `main':
critFarmTest.cpp:(.text+0x8d): undefined reference to `Critter::GetName[abi:cxx11]() const'
/tmp/farmImp-b2c6e5.o: In function `Farm::RollCall() const':
farmImp.cpp:(.text+0xd9): undefined reference to `Critter::GetName[abi:cxx11]()const'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
compiler exit status 1
Remove the "inline" in critterImp.cpp. Not sure if that will fix it but it certainly isn't helping.
If you put GetName in the .cpp file it cannot be inline. Either put it into the .h file or get rid of the inline.
Also, this is probably yet another transcribing error, but:
for (vector::const_iterator iter

It should be vector<Critter>::const_iterator iter
Last edited on
Ok, I have gotten rid of it and it still isn't working. Also, all this code is not mine. It is from my book: Beginning C++ Through Game Programming. Thank you guys for the help so far.
Is it still the same error message or is it a new error message? This matters.
When I used 'vector<Critter>::const_iterator iter' instead of 'for (vector::const_iterator iter', the error messages were the same except they now say ./farm.h:16:5: instead of ./farm.h:15:5:
If farm.h is defining a vector of Critters, it needs to know what a Critter is. You need to #include "Critter.h" inside of farm.h. You cannot rely on a forward declaration.

Why don't you paste your latest code into a new post. Of course, use code tags.

1. Proper indentation and less whitespace might help as well when using code tags.

2. Line 25, "farmpImp.cpp":

for (std::vector<Critter>::const_iterator iter = m_Critters.cbegin();

Notice cbegin() instead of begin(). C++11 has constant iterators for the beginning and end of STL containers.

http://www.cplusplus.com/reference/vector/vector/cbegin/

C++11 makes deducing complex objects like iterators MUCH easier....using auto:

for (auto iter = m_Critters.cbegin();

3. Adding header guards to your header files is recommended.

4. inline functions must be declared and defined in a header file, not in an implementation file (.cpp):

Critter.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef __CRITTER_HPP__
#define __CRITTER_HPP__

#include <iostream>
#include <string>

class Critter
{
public:
   Critter(const std::string& name = "");

public:
   std::string GetName() const;

private:
   std::string m_Name;
};

inline std::string Critter::GetName() const { return m_Name; }

#endif 


Critter.cpp:
1
2
3
#include "Critter.hpp"

Critter::Critter(const std::string& name) :  m_Name(name) { }


5. I prefer using .hpp for C++ header files, .h for C headers:

Farm.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef __FARM_HPP__
#define __FARM_HPP__

#include <iostream>
#include <string>
#include <vector>

#include "Critter.hpp"

class Farm
{
public:
   Farm(int spaces = 1);

public:
   void Add(const Critter& aCritter);
   void RollCall() const;

private:
   std::vector<Critter> m_Critters;

};

#endif 


Farm.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Critter.hpp"
#include "Farm.hpp"

Farm::Farm(int spaces) { m_Critters.reserve(spaces);  }

void Farm::Add(const Critter& aCritter) {  m_Critters.push_back(aCritter); }

void Farm::RollCall() const
{
   for (auto iter = m_Critters.cbegin(); iter != m_Critters.cend(); ++iter)
   {
      std::cout << iter->GetName() << " here.\n";
   }
}


FarmTest.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <vector>
#include <string>

#include "Critter.hpp"
#include "Farm.hpp"

int main()
{
   Critter crit("Poochie");

   std::cout << "My critter's name is " << crit.GetName() << ".\n\n";

   std::cout << "Creating critter farm.\n\n";

   Farm myFarm(3);

   std::cout << "Adding three critters to the farm.\n\n";

   myFarm.Add(Critter("Moe"));
   myFarm.Add(Critter("Larry"));
   myFarm.Add(Critter("Curly"));

   std::cout << "Calling Roll...\n";

   myFarm.RollCall();
}


And the output:
My critter's name is Poochie.

Creating critter farm.

Adding three critters to the farm.

Calling Roll...
Moe here.
Larry here.
Curly here.
Critter.hpp could also be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef __CRITTER_HPP__
#define __CRITTER_HPP__

#include <iostream>
#include <string>

class Critter
{
public:
   Critter(const std::string& name = "");

public:
   std::string GetName() const { return m_Name; }

private:
   std::string m_Name;
};
#endif 


Critter::GeName is now implicitly declared as inline. No need for the member function definition outside the class declaration.
Thank you so much Furry Guy!! That worked perfectly.
Topic archived. No new replies allowed.