By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,136 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,136 IT Pros & Developers. It's quick & easy.

Why still can't compile (simple)

P: n/a
Why still can't compile (simple)

Hi all

I have nothing to say , the code still can not be complie, where's problem?

=========================================
//test.cpp
#include <stdio.h>
#include <string.h>
#include "virtual_screen.h"
int main( int argc, const char* argv[] )
{
VirtualScreen tmp_vs_;

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

tmp_vs_ << i_a << str_tmp;

return 0;
}

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

#include <string.h>

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

#endif //VIRTUAL_SCREEN_H

==========================================
//virtual_screen.cpp

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

void
VirtualScreen::
printf_char(std::string ch)
{
fprintf(stdout,ch.c_str ());
}
VirtualScreen& operator<< (VirtualScreen& p_vs, std::string p_String)
{
std::stringstream Temp;
Temp << p_String;
p_vs.print_char (Temp.str());
return p_vs;
}

VirtualScreen& operator<< (VirtualScreen& p_vs, int p_Number)
{
std::stringstream Temp;
Temp << p_Number;
p_vs.print_char (Temp.str());
return p_vs;
}

===========================================
please give the complie command string
I use
"gcc -o test test.cpp virtual_screen.cpp -lstdc++"
"gcc -o test test.cpp virtual_screen.cpp"

I am sad ......:(

thank you very
much

key9
Sep 13 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
key9 wrote:
Why still can't compile (simple)

Hi all

I have nothing to say , the code still can not be complie, where's
problem?
You should know _where_ it is (even if you don't know _what_ it is). I would
be very surprised if the compiler didn't tell you. So please copy the error
message you got from the compiler into your posting and mark the line in
your code that it refers to.
I use
"gcc -o test test.cpp virtual_screen.cpp -lstdc++"
"gcc -o test test.cpp virtual_screen.cpp"
You should use g++ for C++ code and not link libstdc++ explicitly.

Sep 13 '06 #2

P: n/a
sorry for that: here 's the output
I 've got no idea why here's so much unreadable char , either in tty or
console

g++ -o test test.cpp virtual_screen.cpp
virtual_screen.h:11: error: ‚?~std::string‚?T has not been declared
test.cpp:7: error: new types may not be defined in a return type
test.cpp:7: note: (perhaps a semicolon is missing after the definition of
‚?~VirtualScreen‚?T)
test.cpp:7: error: two or more data types in declaration of ‚?~main‚?T
test.cpp:7: error: ‚?~::main‚?T must return ‚?~int‚?T
test.cpp: In function ‚?~int main(int, const char**)‚?T:
test.cpp:12: error: ‚?~string‚?T was not declared in this scope
test.cpp:12: error: expected `;' before ‚?~str_tmp‚?T
test.cpp:14: error: no match for ‚?~operator<<‚?T in ‚?~tmp_vs_ <<
i_a‚?T
test.cpp:14: error: ‚?~str_tmp‚?T was not declared in this scope
virtual_screen.cpp:4:21: error: sstream.h: No such file or directory
virtual_screen.h:11: error: ‚?~std::string‚?T has not been declared
virtual_screen.cpp:8: error: two or more data types in declaration of
‚?~printf_char‚?T
virtual_screen.cpp:8: error: ‚?~VirtualScreen VirtualScreen::printf_char‚?T
is not a static member of ‚?~class VirtualScreen‚?T
virtual_screen.cpp:8: error: ‚?~string‚?T is not a member of ‚?~std‚?T
virtual_screen.cpp:9: error: expected ‚?~,‚?T or ‚?~;‚?T before ‚?~{‚?T
token
virtual_screen.cpp:15: error: ‚?~std::string‚?T has not been declared
virtual_screen.cpp: In function ‚?~VirtualScreen& operator<<(VirtualScreen&,
int)‚?T:
virtual_screen.cpp:18: error: ‚?~stringstream‚?T is not a member of
‚?~std‚?T
virtual_screen.cpp:18: error: expected `;' before ‚?~Temp‚?T
virtual_screen.cpp:19: error: ‚?~Temp‚?T was not declared in this scope
virtual_screen.cpp:20: error: ‚?~class VirtualScreen‚?T has no member named
‚?~print_char‚?T
virtual_screen.cpp: In function ‚?~VirtualScreen& operator<<(VirtualScreen&,
int)‚?T:
virtual_screen.cpp:25: error: redefinition of ‚?~VirtualScreen&
operator<<(VirtualScreen&, int)‚?T
virtual_screen.cpp:15: error: ‚?~VirtualScreen& operator<<(VirtualScreen&,
int)‚?T previously defined here
virtual_screen.cpp:29: error: ‚?~stringstream‚?T is not a member of
‚?~std‚?T
virtual_screen.cpp:29: error: expected `;' before ‚?~Temp‚?T
virtual_screen.cpp:30: error: ‚?~Temp‚?T was not declared in this scope
virtual_screen.cpp:31: error: ‚?~class VirtualScreen‚?T has no member named
‚?~print_char‚?T[0

Sep 13 '06 #3

P: n/a
I am sorry , I think I've lost my mind just now
after currect my code to this
only 2 mistake
g++ -o test test.cpp virtual_screen.cpp
virtual_screen.h:15: error: expected initializer before ‚?~&‚?T token
test.cpp: In function ‚?~int main(int, const char**)‚?T:
test.cpp:14: error: no match for ‚?~operator<<‚?T in
‚?~operator<<(((VirtualScreen&)(& tmp_vs_)), i_a) << str_tmp‚?T
virtual_screen.h:17: note: candidates are: VirtualScreen&
operator<<(VirtualScreen&, int)
virtual_screen.h:15: error: expected initializer before ‚?~&‚?T 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;
}


Sep 13 '06 #4

P: n/a
key9 wrote:
sorry for that: here 's the output
I 've got no idea why here's so much unreadable char , either in tty or
console

>g++ -o test test.cpp virtual_screen.cpp
virtual_screen.h:11: error: ‚?~std::string‚?T has not been declared
test.cpp:7: error: new types may not be defined in a return type
test.cpp:7: note: (perhaps a semicolon is missing after the definition of
‚?~VirtualScreen‚?T)
test.cpp:7: error: two or more data types in declaration of ‚?~main‚?T
test.cpp:7: error: ‚?~::main‚?T must return ‚?~int‚?T
test.cpp: In function ‚?~int main(int, const char**)‚?T:
test.cpp:12: error: ‚?~string‚?T was not declared in this scope
test.cpp:12: error: expected `;' before ‚?~str_tmp‚?T
test.cpp:14: error: no match for ‚?~operator<<‚?T in ‚?~tmp_vs_ <<
i_a‚?T
test.cpp:14: error: ‚?~str_tmp‚?T was not declared in this scope
virtual_screen.cpp:4:21: error: sstream.h: No such file or directory
virtual_screen.h:11: error: ‚?~std::string‚?T has not been declared
virtual_screen.cpp:8: error: two or more data types in declaration of
‚?~printf_char‚?T
virtual_screen.cpp:8: error: ‚?~VirtualScreen VirtualScreen::printf_char‚?T
is not a static member of ‚?~class VirtualScreen‚?T
virtual_screen.cpp:8: error: ‚?~string‚?T is not a member of ‚?~std‚?T
virtual_screen.cpp:9: error: expected ‚?~,‚?T or ‚?~;‚?T before ‚?~{‚?T
token
virtual_screen.cpp:15: error: ‚?~std::string‚?T has not been declared
virtual_screen.cpp: In function ‚?~VirtualScreen& operator<<(VirtualScreen&,
int)‚?T:
virtual_screen.cpp:18: error: ‚?~stringstream‚?T is not a member of
‚?~std‚?T
virtual_screen.cpp:18: error: expected `;' before ‚?~Temp‚?T
virtual_screen.cpp:19: error: ‚?~Temp‚?T was not declared in this scope
virtual_screen.cpp:20: error: ‚?~class VirtualScreen‚?T has no member named
‚?~print_char‚?T
virtual_screen.cpp: In function ‚?~VirtualScreen& operator<<(VirtualScreen&,
int)‚?T:
virtual_screen.cpp:25: error: redefinition of ‚?~VirtualScreen&
operator<<(VirtualScreen&, int)‚?T
virtual_screen.cpp:15: error: ‚?~VirtualScreen& operator<<(VirtualScreen&,
int)‚?T previously defined here
virtual_screen.cpp:29: error: ‚?~stringstream‚?T is not a member of
‚?~std‚?T
virtual_screen.cpp:29: error: expected `;' before ‚?~Temp‚?T
virtual_screen.cpp:30: error: ‚?~Temp‚?T was not declared in this scope
virtual_screen.cpp:31: error: ‚?~class VirtualScreen‚?T has no member named
‚?~print_char‚?T[0
string.h nor sstream.h are not standard headers

Now you must use string and sstream.

>

Sep 13 '06 #5

P: n/a
key9 wrote:
sorry for that: here 's the output
I 've got no idea why here's so much unreadable char , either in tty or
console
Seems your console doesn't use the same character set as your compiler.
>g++ -o test test.cpp virtual_screen.cpp
virtual_screen.h:11: error: ‚?~std::string‚?T has not been declared
Well, the compiler is right. You #included the wrong header. Try <string>
instead of <string.h>.
test.cpp:7: error: new types may not be defined in a return type
test.cpp:7: note: (perhaps a semicolon is missing after the definition of
‚?~VirtualScreen‚?T)
test.cpp:7: error: two or more data types in declaration of
‚?~main‚?T test.cpp:7: error: ‚?~::main‚?T must return
‚?~int‚?T
Those messages probably all result from a missing semicolon at the end of
your class definition in virtual_screen.h. The second one already tells you
exactly that.
test.cpp: In function ‚?~int main(int, const char**)‚?T:
test.cpp:12: error: ‚?~string‚?T was not declared in this scope
test.cpp:12: error: expected `;' before ‚?~str_tmp‚?T
Those have the same reason as the first few messages (and you forgot to use
the std:: prefix for the type on your definition of str_tmp).
test.cpp:14: error: no match for ‚?~operator<<‚?T in ‚?~tmp_vs_ <<
i_a‚?T
Indeed, you didn't declare that operator anywhwere in your header.
test.cpp:14: error: ‚?~str_tmp‚?T was not declared in this scope
Another result of the compiler not finding the string class.
virtual_screen.cpp:4:21: error: sstream.h: No such file or directory
The header name you want is <sstring>.

All the other error messages seem to be results from the errors mentioned
above.

Sep 13 '06 #6

P: n/a
Carlos Martinez wrote:
>
string.h nor sstream.h are not standard headers
string.h is a standard header, but it's the wrong one.
Sep 13 '06 #7

P: n/a

key9 wrote:
Why still can't compile (simple)

Hi all

I have nothing to say , the code still can not be complie, where's problem?

=========================================
//test.cpp
#include <stdio.h>
#include <string.h>
#include "virtual_screen.h"
int main( int argc, const char* argv[] )
{
VirtualScreen tmp_vs_;

int i_a = 100;
string str_tmp = "This is test string";
unless virtual_screen.h brings in std::string then you haven't brought
it in at all. You should include <stringnot <string.h>. You should
include <cstdioin preference to <stdio.hbut I don't see why you
need to include that file at all.
tmp_vs_ << i_a << str_tmp;

return 0;
}

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

#include <string.h>

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

#endif //VIRTUAL_SCREEN_H
You did operator<< with VirtualScreen above but there is no operator<<
declared with a RHS of VirtualScreen and a LHS of int. You define it
later on but you need a prototype.

You should be including <stringnot <string.h>. Your function should
probably take const std::string & as its parameter type but that's not
essential. You might also want to make the function const. (It can't
change the state of VirtualScreen as VirtualScreen doesn't have any
member variables).
==========================================
//virtual_screen.cpp

#include <stdio.h>
#include <string.h>
#include "virtual_screen.h"
#include <sstream.h>
Headers again.
void
VirtualScreen::
printf_char(std::string ch)
{
fprintf(stdout,ch.c_str ());
}
Note that the string could contain sequences like %s or %c in it which
would invoke undefined behaviour as fprintf would look for latter
parameters that aren't there. fputs would be a better option. Why use
printf at all though? What's wrong with cout? Now if you were doing
formatted output I could forgive you for using fprintf, although
boost::format is a good alternative.
VirtualScreen& operator<< (VirtualScreen& p_vs, std::string p_String)
{
std::stringstream Temp;
Temp << p_String;
p_vs.print_char (Temp.str());
return p_vs;
}
Pointless use of stringstream when all you do is convert back to the
string you started off with. You streamed it for practice?
VirtualScreen& operator<< (VirtualScreen& p_vs, int p_Number)
{
std::stringstream Temp;
Temp << p_Number;
p_vs.print_char (Temp.str());
return p_vs;
}
This one does make sense. Note you could make operator<< a template.

Essentially, to get it to compile, use the correct headers and put in a
prototype to your operator << overloads in the header.

Sep 13 '06 #8

P: n/a
key9 a ťcrit :
I am sorry , I think I've lost my mind just now
after currect my code to this
only 2 mistake
class VirtualScreen
{
public:
void printf_char(std::string ch);

}
A semicolon is needed here.

VirtualScreen& operator<< (VirtualScreen& p_vs, std::string p_String);
//line 15
--
Serge Paccalin
<se************@easyvisio.net>
Sep 13 '06 #9

P: n/a
key9 wrote:
sorry for that: here 's the output
I 've got no idea why here's so much unreadable char , either in tty or
console
Probably colorgcc. The solution would be to install the missing
packages to allow color coded output on the console, but that's
offtopic on this list.
g++ -o test test.cpp virtual_screen.cpp
virtual_screen.h:11: error: ‚?~std::string‚?T has not been declared
Let me guess, you've been stuck away on some non c++ island for the
last ten years ;-)

Nowadays we don't use
#include <string.h>
we use
#include <string>

for ALL standard headers. Didn't bother to look at the rest, maybe it's
ok ;-)

Sep 13 '06 #10

P: n/a

Rolf Magnus wrote:
key9 wrote:
virtual_screen.cpp:4:21: error: sstream.h: No such file or directory

The header name you want is <sstring>.
Actually it's <sstream>

Sep 13 '06 #11

P: n/a
"F.J.K." <fe***********@gmail.comwrote in message
news:11**********************@e63g2000cwd.googlegr oups.com...

Nowadays we don't use
#include <string.h>
we use
#include <string>

for ALL standard headers. Didn't bother to look at the rest, maybe it's
ok ;-)

[pjp] No, we use:

-- <string.hto declare the C string functions in the global namespace

-- <cstringto declare the C string functions in namespace std

-- <stringto declare template class basic_string

All are part of Standard C++.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Sep 13 '06 #12

P: n/a
Earl Purple wrote:
>
Rolf Magnus wrote:
>key9 wrote:
virtual_screen.cpp:4:21: error: sstream.h: No such file or directory

The header name you want is <sstring>.
Actually it's <sstream>
Oops... my bad. You're right of course.
Sep 13 '06 #13

P: n/a
P.J. Plauger wrote:
[pjp] No, we use:

-- <string.hto declare the C string functions in the global namespace

-- <cstringto declare the C string functions in namespace std

-- <stringto declare template class basic_string

All are part of Standard C++.
Thanks for clarification. Just out of historical interest. Wasn't there
a time in earliest C++, when including <string.hwould give you class
std::string and including <cstring.hwould give you std::strcat and
it's merry bunch?

Sep 15 '06 #14

P: n/a
P.J. Plauger wrote:
[pjp] No, we use:

-- <string.hto declare the C string functions in the global namespace

-- <cstringto declare the C string functions in namespace std

-- <stringto declare template class basic_string

All are part of Standard C++.
Thanks for clarification. Just out of historical interest. Wasn't there
a time in earliest C++, when including <string.hwould give you class
std::string and including <cstring.hwould give you std::strcat and
it's merry bunch?

Sep 15 '06 #15

P: n/a
In article <11********************@i3g2000cwc.googlegroups.co m>,
fe***********@gmail.com says...

[ ... ]
Thanks for clarification. Just out of historical interest. Wasn't there
a time in earliest C++, when including <string.hwould give you class
std::string and including <cstring.hwould give you std::strcat and
it's merry bunch?
Certainly not with any compiler I ever used, and I rather doubt it
happened with any compiler, period. From the very beginning, C++
coexisted with C, so all the C headers remained nearly intact. The sole
change to most of them was adding extern "C" to the declarations. A few
also had to change a few minor details, such as defining NULL as an
integer type instead of a pointer to void.

I believe the <c*headers were invented by the committee -- no early
compiler I ever used included them at all.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 15 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.