Hi Guys, I'm wondering if someone can help me out with a problem I've been having with VC++. I'm obviously just beginning to learn C++ and have been working with CodeBlocks and VC++ 2010 Express. My problem is that the following code compiles in CodeBlocks but gives me numerous errors and dies with VC++ (for the sake of space, I've simplified it as much as I can down to the problem area)...

In Main.cpp:

#include "Head.h"

int main()
{
cout << strFunction(12) << endl;
return 0;
}

In Main2.cpp:

#include "Head.h"

string strFunction(int x)
{
return "blah blah blah.";
}

In Head.h:

#pragma once
#include <iostream>
using namespace std;
string strFunction(int x);

Since I'm only dealing with two source files and a miniscule program, obviously a header here isnt really necessary, but I really want to learn how to work with them for later use. The maddening thing about this is:

1) The header works for some things. I use it to #include <iostream> without any trouble, I can declare global variables just fine, etc. Its really just something with any function prototypes I put in there.

2) Function prototypes work fine if I just manually copy the prototype into the other source file. I only get the errors when I reference it in the header.

Here is (a small part) of the errors I receive when I try and run the above project:

Main.cpp
1>c:\users\michael\documents\visual studio 2010\projects\project7\project7\main.cpp(5): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
1> c:\program files\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\ostream(726): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\ostream(764): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'

----

I'd be tremendously appreciative of any help you guys could offer me. I've tried everything I could think of and experimented with everything I can think of but nothing seems to work. FWIW, I've tried this code in a completely empty VC++ project as well as inputting it into their custom made console app. Same errors.

I'm at my wits end. Please help.

(p.s. first post, great to be on board here).

You forgot to include <string>.

Ah! Thanks so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.