954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

VC++ Headers

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 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).

civus
Newbie Poster
2 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

You forgot to include .

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Ah! Thanks so much!

civus
Newbie Poster
2 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: