"string could not be resolved"

Dec 4, 2011 at 2:36am
here is the code, is get an error "Type string (or std::string) could not be resolved"

btw i know this doestn do anything im stil declaring this stuff and i need to be ab le to use strings before i start implementation


#include <iostream>
#include <fstream>
#include <sstream>


class Musicfile{ public:

Musicfile();
~Musicfile(); //default destructor w/ no argument

void get_info();
void print_info();
};
Last edited on Dec 4, 2011 at 2:37am
Dec 4, 2011 at 2:48am
Try #include <string> if you are going to use string.
Dec 4, 2011 at 2:54am
i still get the same error
Dec 4, 2011 at 2:58am
your code don't use string so I don't understand why you get that error
Dec 4, 2011 at 3:05am
no i get the error when i try to declare a string, i havent done so in the code above but when i do i get the error
Dec 4, 2011 at 3:12am
does this compile?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

class Musicfile{ public:

Musicfile();
~Musicfile(); //default destructor w/ no argument

void get_info();
void print_info();

std::string str;
}; 
Dec 4, 2011 at 3:38am
nope, ame error, btw i can do ints chars, and other primitive data types and it works fine, but i cant use a char array for what i want to do
Dec 31, 2011 at 10:06am
This thread seems to have died away. Let me attempt to revive it.

Here is the code

File: "name.hpp"

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef NAME_HPP_
#define NAME_HPP_

#include <string>

class Name {
private:
	string name;
public:
	Name();
};

#endif /* NAME_HPP_ */ 


File: "name.cpp"

1
2
3
4
#include "name.hpp"

Name::Name() {
}



And the errors:

Description	Resource	Path	Location	Type
'string' does not name a type	name.hpp	/hw	line 15	C/C++ Problem
Type 'string' could not be resolved	name.hpp	/hw	line 15	Semantic Error



WTF IS WRONG WITH THIS CODE? (sorry to shout, this is frustrating)

As it so happens, if I copy it to the file where the "main()" function is located, then it's perfectly happy.
Dec 31, 2011 at 10:20am
closed account (DSLq5Di1)
Missing the namespace declaration,

1
2
3
4
5
#include <string>

class Name {
private:
	std::string name;
Dec 31, 2011 at 10:34am
AHHH! It's so simple when you see it!

Thank you @sloppy9, that fixes it (or including a "using namespace std" line)
Dec 31, 2011 at 11:16am
you can also try the namespace option with the above code .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std; 
class Musicfile{ 
public:

        Musicfile();
        ~Musicfile(); //default destructor w/ no argument

          void get_info();
          void print_info();

          std::string str;
}; 

Jan 1, 2012 at 12:03am
Thank you @bluecoder! Any particular reason to include both the "using namespace std" directive and the "std::" scope identifier?

I was getting to ready to come in screaming and yelling, because I tried it again on a different machine, but this time I was getting "'string' does not name a type".

I was all up and typing in my complaint, when I noticed the examples above: "#include <string>".

Oops! A little something I seemed to have forgotten!

So much to remember!

Jan 1, 2012 at 12:08am
There is no reason to use both unless you have a bad habit of alternating whether you use std:: or not during the same code session.
Jan 1, 2012 at 12:08am
Although using namespace std; can be useful for trivial code examples, it should generally be avoided in full programs:
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

It can be the source of some difficult to find errors.
Topic archived. No new replies allowed.