| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"key9" |
| Date: |
13 Sep 2006 04:38:59 AM |
| Object: |
Why still can't compile (simple) |
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
.
|
|
| User: "Rolf Magnus" |
|
| Title: Re: Why still can't compile (simple) |
13 Sep 2006 05:07:53 AM |
|
|
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.
.
|
|
|
| User: "key9" |
|
| Title: Re: Why still can't compile (simple) |
13 Sep 2006 06:25:18 AM |
|
|
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[0m
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;
}
.
|
|
|
| User: "Serge Paccalin" |
|
| Title: Re: Why still can't compile (simple) |
13 Sep 2006 07:35:56 AM |
|
|
key9 a =E9crit :
I am sorry , I think I've lost my mind just now
after currect my code to this
only 2 mistake
=20
=20
class VirtualScreen
{
public:
void printf_char(std::string ch);
=20
}
A semicolon is needed here.
=20
VirtualScreen& operator<< (VirtualScreen& p_vs, std::string p_String); =
//line 15
--=20
Serge Paccalin
<serge.paccalin@easyvisio.net>
.
|
|
|
|
|
| User: "key9" |
|
| Title: Re: Why still can't compile (simple) |
13 Sep 2006 05:46:28 AM |
|
|
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[0m
test.cpp:7: error: â?~::mainâ?T must return â?~intâ?T[0m
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[0m
test.cpp:14: error: no match for â?~operator<<â?T in â?~tmp_vs_ <<
i_aâ?T[0m
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[0m
virtual_screen.cpp:8: error: â?~VirtualScreen VirtualScreen::printf_charâ?T
is not a static member of â?~class VirtualScreenâ?T[0m
virtual_screen.cpp:8: error: â?~stringâ?T is not a member of â?~stdâ?T[0m
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[0m
virtual_screen.cpp:18: error: expected `;' before â?~Tempâ?T[0m
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[0m
virtual_screen.cpp: In function â?~VirtualScreen& operator<<(VirtualScreen&,
int)â?T:
virtual_screen.cpp:25: error: redefinition of â?~VirtualScreen&
operator<<(VirtualScreen&, int)â?T[0m
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[0m
virtual_screen.cpp:29: error: expected `;' before â?~Tempâ?T[0m
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
.
|
|
|
| User: "F.J.K." |
|
| Title: Re: Why still can't compile (simple) |
13 Sep 2006 08:28:20 AM |
|
|
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: =E2?~std::string=E2?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 ;-)
.
|
|
|
| User: "P.J. Plauger" |
|
| Title: Re: Why still can't compile (simple) |
13 Sep 2006 11:22:37 AM |
|
|
"F.J.K." <felix.koehler@gmail.com> wrote in message
news:1158154100.896670.194540@e63g2000cwd.googlegroups.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.h> to declare the C string functions in the global namespace
-- <cstring> to declare the C string functions in namespace std
-- <string> to declare template class basic_string
All are part of Standard C++.
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
.
|
|
|
| User: "F.J.K." |
|
| Title: Re: Why still can't compile (simple) |
15 Sep 2006 07:38:50 AM |
|
|
P.J. Plauger wrote:
[pjp] No, we use:
-- <string.h> to declare the C string functions in the global namespace
-- <cstring> to declare the C string functions in namespace std
-- <string> to 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.h> would give you class
std::string and including <cstring.h> would give you std::strcat and
it's merry bunch?
.
|
|
|
|
| User: "F.J.K." |
|
| Title: Re: Why still can't compile (simple) |
15 Sep 2006 07:38:56 AM |
|
|
P.J. Plauger wrote:
[pjp] No, we use:
-- <string.h> to declare the C string functions in the global namespace
-- <cstring> to declare the C string functions in namespace std
-- <string> to 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.h> would give you class
std::string and including <cstring.h> would give you std::strcat and
it's merry bunch?
.
|
|
|
| User: "Jerry Coffin" |
|
| Title: Re: Why still can't compile (simple) |
15 Sep 2006 10:13:57 AM |
|
|
In article <1158323936.158967.9600@i3g2000cwc.googlegroups.com>,
felix.koehler@gmail.com says...
[ ... ]
Thanks for clarification. Just out of historical interest. Wasn't there
a time in earliest C++, when including <string.h> would give you class
std::string and including <cstring.h> would 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.
.
|
|
|
|
|
|
|
| User: "Rolf Magnus" |
|
| Title: Re: Why still can't compile (simple) |
13 Sep 2006 06:28:52 AM |
|
|
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[0m test.cpp:7: error: â?~::mainâ?T must return
â?~intâ?T[0m
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[0m
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[0m
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.
.
|
|
|
| User: "Earl Purple" |
|
| Title: Re: Why still can't compile (simple) |
13 Sep 2006 08:32:48 AM |
|
|
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>
.
|
|
|
| User: "Rolf Magnus" |
|
| Title: Re: Why still can't compile (simple) |
13 Sep 2006 11:38:39 AM |
|
|
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.
.
|
|
|
|
|
|
| User: "Carlos Martinez" |
|
| Title: Re: Why still can't compile (simple) |
13 Sep 2006 05:54:57 AM |
|
|
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[0m
test.cpp:7: error: â?~::mainâ?T must return â?~intâ?T[0m
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[0m
test.cpp:14: error: no match for â?~operator<<â?T in â?~tmp_vs_ <<
i_aâ?T[0m
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[0m
virtual_screen.cpp:8: error: â?~VirtualScreen VirtualScreen::printf_charâ?T
is not a static member of â?~class VirtualScreenâ?T[0m
virtual_screen.cpp:8: error: â?~stringâ?T is not a member of â?~stdâ?T[0m
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[0m
virtual_screen.cpp:18: error: expected `;' before â?~Tempâ?T[0m
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[0m
virtual_screen.cpp: In function â?~VirtualScreen& operator<<(VirtualScreen&,
int)â?T:
virtual_screen.cpp:25: error: redefinition of â?~VirtualScreen&
operator<<(VirtualScreen&, int)â?T[0m
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[0m
virtual_screen.cpp:29: error: expected `;' before â?~Tempâ?T[0m
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.
.
|
|
|
| User: "Pete Becker" |
|
| Title: Re: Why still can't compile (simple) |
13 Sep 2006 06:37:37 AM |
|
|
Carlos Martinez wrote:
string.h nor sstream.h are not standard headers
string.h is a standard header, but it's the wrong one.
.
|
|
|
|
|
|
|
| User: "Earl Purple" |
|
| Title: Re: Why still can't compile (simple) |
13 Sep 2006 06:38:16 AM |
|
|
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 <string> not <string.h>. You should
include <cstdio> in preference to <stdio.h> but 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 <string> not <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.
.
|
|
|
|

|
Related Articles |
|
|