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

K

key9

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
 
N

Nick Keighley

key9 said:
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

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?
 
M

Murali Krishna

key9 said:
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
 
P

Pete Becker

Murali said:
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.
 
K

key9

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!
 
J

Jerry Coffin

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

David Harmon

On Thu, 14 Sep 2006 01:06:34 +0800 in comp.lang.c++, "key9"
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.
 
P

Pete Becker

David said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top