Including an external class

May 11, 2013 at 5:08pm
This is the simplest problem and yet I have been searching for the last two hours and can not find a single source showing how to do this.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include "ex_lib/ex_lib.h"
using namespace std;

int main()
{
  string_container test;
  test.container = "TEST";
  cout << test.container;

  return 0;
}


ex_lib/ex_lib.h
1
2
3
4
5
6
7
#include <string>

class string_container
{
  public:
  string container;
};


This whole structure works if I make container an int instead. It just doesn't recognize string as an object in the external file for some reason. The error I am getting is 'string' does not name a type
May 11, 2013 at 5:15pm
Try std::string.
May 11, 2013 at 5:16pm
i think the problem is that you have to use std::string container instead of string container in the header file.

Edit: sorry Vidminas, didn't see your post.
Last edited on May 11, 2013 at 5:17pm
May 11, 2013 at 5:19pm
You guys were right. It works now. I had no idea string was part of the std namespace. Thank you for the help. I was looking in all the wrong places for answers.
May 11, 2013 at 5:27pm
There are a lot of things in std than cout and cin try to avoid using namespace std and use std:: and youcan get an idea
May 11, 2013 at 6:15pm
You don't have to type std::cout or std::cin everytime you use them in a program. You can also add
using std::cout; using std::cin; at the top of the code instead of using namespace std; (you might still need to add the std:: prefix to things like string or vector, or endl, but you can also write using statements for them too)
May 11, 2013 at 7:38pm
yeah I was just letting him know that the std namespace has more than just cout and cin in it. and if you do your method with the using std::WhatEver
you should only put that in the function/scope you are actually using it in and not the global scope but if you do use that in the global scope it is atleast better than the whole namespace using namespace std;
Topic archived. No new replies allowed.