Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question about .h file and .cpp file,also compiler question

Reply
Thread Tools

question about .h file and .cpp file,also compiler question

 
 
key9
Guest
Posts: n/a
 
      09-13-2006
Hi all


I think .h file offord a "catalog" of symbols and .cpp offord implements

but I have no idea why I got such an complier error

> g++ -o test test.cpp virtual_screen.cpp

virtual_screen.h:15: error: expected initializer before a?&a? token
test.cpp: In function a?int main(int, const char**)a?:
test.cpp:14: error: no match for a?operator<<a? in
a?operator<<(((VirtualScreen&)(& tmp_vs_)), i_a) << str_tmpa?
virtual_screen.h:17: note: candidates are: VirtualScreen&
operator<<(VirtualScreen&, int)
virtual_screen.h:15: error: expected initializer before a?&a? token

virtual_screen.h
================================================== =============
#ifndef VIRTUAL_SCREEN_H
#define VIRTUAL_SCREEN_H


#include <string>


class VirtualScreen
{
public:
void printf_char(std::string ch);

}

VirtualScreen& operator<< (VirtualScreen& p_vs, std::string p_String);
//line 15

VirtualScreen& operator<< (VirtualScreen& p_vs, int p_Number); //
line 17

#endif //VIRTUAL_SCREEN_H

virtual_screen.cpp
================================================== ==============
#include <stdio.h>
#include <string>
#include "virtual_screen.h"
#include <sstream>

class VirtualScreen;

void
VirtualScreen::
printf_char(std::string ch)
{
fprintf(stdout,ch.c_str());
}

/* sample of over ride */

VirtualScreen& operator<< (VirtualScreen& p_vs, std::string p_String)
{
// Let the terminal print the string as it is.
std::stringstream Temp;
Temp << p_String;
p_vs.printf_char (Temp.str());
return p_vs;
}


VirtualScreen& operator<< (VirtualScreen& p_vs, int p_Number)
{
// Convert the number to a string and let the terminal print the
// text representation of the number.
std::stringstream Temp;
Temp << p_Number;
p_vs.printf_char (Temp.str());
return p_vs;
}

================================================== ====================
test.cpp
#include <stdio.h>
#include <string>
#include "virtual_screen.h"



int main( int argc, const char* argv[] )
{
VirtualScreen tmp_vs_;

int i_a = 100;
std::string str_tmp = "This is test string";

tmp_vs_ << i_a << str_tmp;

return 0;


}

sorry for my last stupid post , I've just lost my mind.

thank you very
much

key9

 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      09-13-2006
key9 wrote:

> I think .h file offord a "catalog" of symbols and .cpp offord implements
>
> but I have no idea why I got such an complier error
>
> > g++ -o test test.cpp virtual_screen.cpp

> virtual_screen.h:15: error: expected initializer before a?&a? token
> test.cpp: In function a?int main(int, const char**)a?:


I don't know why the euro symbols... I've noted one error in your code.
There may be others. In general if a compiler complains about code that

looks ok it may be confused, take a look at the line *before* the
complaint.


> test.cpp:14: error: no match for a?operator<<a? in
> a?operator<<(((VirtualScreen&)(& tmp_vs_)), i_a) << str_tmpa?
> virtual_screen.h:17: note: candidates are: VirtualScreen&
> operator<<(VirtualScreen&, int)
> virtual_screen.h:15: error: expected initializer before a?&a? token
>
> virtual_screen.h
> ================================================== =============
> #ifndef VIRTUAL_SCREEN_H
> #define VIRTUAL_SCREEN_H
>
>
> #include <string>
>
>
> class VirtualScreen
> {
> public:
> void printf_char(std::string ch);
>
> }


semi-colon here



> VirtualScreen& operator<< (VirtualScreen& p_vs, std::string p_String);
> //line 15
>
> VirtualScreen& operator<< (VirtualScreen& p_vs, int p_Number); //
> line 17
>
> #endif //VIRTUAL_SCREEN_H



<snip>


--
Nick Keighley

De maan likt niet hoog The moon doesn't look high
Maar het is niet zo But it is not so
De maan is wel hoog The moon is very high
Of niet sams? Or is it?

 
Reply With Quote
 
 
 
 
Murali Krishna
Guest
Posts: n/a
 
      09-13-2006

key9 wrote:
> virtual_screen.h
> ================================================== =============
> #ifndef VIRTUAL_SCREEN_H
> #define VIRTUAL_SCREEN_H
>
>
> #include <string>
>
>
> class VirtualScreen
> {
> public:
> void printf_char(std::string ch);
>
> }
>


Yes as Nick wrote there has to be a semi colon at the end of the class.
I don't know why your compiler did not show that.

-- Murali Krishna

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      09-13-2006
Murali Krishna wrote:
> key9 wrote:
>> virtual_screen.h
>> ================================================== =============
>> #ifndef VIRTUAL_SCREEN_H
>> #define VIRTUAL_SCREEN_H
>>
>>
>> #include <string>
>>
>>
>> class VirtualScreen
>> {
>> public:
>> void printf_char(std::string ch);
>>
>> }
>>

>
> Yes as Nick wrote there has to be a semi colon at the end of the class.
> I don't know why your compiler did not show that.
>


A semicolon is not required at the end of a class definition.

class C
{
}

c;

This defines an object named "c" whose type is C.

class C
{
}

int i;

This is an error which can be fixed by adding a semicolon after the
class definition.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
Reply With Quote
 
key9
Guest
Posts: n/a
 
      09-13-2006
the problem solved, thanks ; but I am still confuse.
I still don't know if a semicolon is required at end of class defination.

but here the problem actually solved , I can focus on my mind.

thank all of you very much!


"Pete Becker" <(E-Mail Removed)> ????
news:(E-Mail Removed) ...
> Murali Krishna wrote:
>> key9 wrote:
>>> virtual_screen.h
>>> ================================================== =============
>>> #ifndef VIRTUAL_SCREEN_H
>>> #define VIRTUAL_SCREEN_H
>>>
>>>
>>> #include <string>
>>>
>>>
>>> class VirtualScreen
>>> {
>>> public:
>>> void printf_char(std::string ch);
>>>
>>> }
>>>

>>
>> Yes as Nick wrote there has to be a semi colon at the end of the class.
>> I don't know why your compiler did not show that.
>>

>
> A semicolon is not required at the end of a class definition.
>
> class C
> {
> }
>
> c;
>
> This defines an object named "c" whose type is C.
>
> class C
> {
> }
>
> int i;
>
> This is an error which can be fixed by adding a semicolon after the class
> definition.
>
> --
>
> -- Pete
>
> Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
> For more information about this book, see www.petebecker.com/tr1book.



 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      09-13-2006
In article <ee9dqr$bfa$(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed) says...
> the problem solved, thanks ; but I am still confuse.
> I still don't know if a semicolon is required at end of class defination.


Consider something like this:

struct point {
int x, y;
} point, point2;

You have 'struct', an optional name, the body, a closing brace, and then
an optional list of objects being defined of that type, then a semicolon
to tell the compiler it's reached the end of the list. Even if you don't
define any objects of the type:

struct point {
int x, y;
};

you still need the semicolon there so the compiler knows that the next
name(s) it sees aren't supposed to be interpreted as a list of objects
to be defined of that type.

A class is just a struct with the default access set to private instead
of public. You can still do the same things otherwise, and you still
need the semicolon to signal the end of the list of objects being
defined (even if it's empty).

You don't need a semicolon after something like a namespace definition
-- you can't create an object with a type of 'namespace whatever'. Since
you _can't_ include a list of objects to be created, there's no need to
signal the end of the list either.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      09-13-2006
On Thu, 14 Sep 2006 01:06:34 +0800 in comp.lang.c++, "key9"
<(E-Mail Removed)> wrote,
>the problem solved, thanks ; but I am still confuse.
>I still don't know if a semicolon is required at end of class defination.


The class definition is a declaration. You must have a semicolon at
the end of the whole declaration.

Pete's statement that it isn't needed at the end of the class
definition is more confusing than necessary. Yes, there can be
other things declared between the end of the class definition and
the semicolon. The semicolon must still be there at the end of it
all.

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      09-13-2006
David Harmon wrote:
>
> Pete's statement that it isn't needed at the end of the class
> definition is more confusing than necessary.


I didn't say it isn't needed. Clearly there are times when it is needed.
I said it isn't required, that is, there are times when it is not needed
and, indeed, it would be an error to put it in. That was the point of
the two examples that I also gave.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Are there any differences between 'Synplicity VHDL compiler, v1.0, b. 074R' and GHDL compiler? Merciadri Luca VHDL 5 10-14-2010 08:40 PM
Compiler Error Message: The compiler failed with error code -1073741819 Ram ASP .Net 0 09-13-2005 09:52 AM
Compiler compiler with C++ as output Andrey Batyuck C++ 3 05-17-2004 08:17 PM
Can we use <compiler> tag to avoid RunTime Compiler error? Jack Wright ASP .Net 5 01-19-2004 04:36 PM
Compiler Error Message: The compiler failed with error code 128. Yan ASP .Net 0 07-21-2003 10:49 PM



Advertisments