Why still can't compile (simple)

K

key9

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
 
R

Rolf Magnus

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

key9

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
 
C

Carlos Martinez

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

key9

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;


}
 
R

Rolf Magnus

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

Earl Purple

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

Serge Paccalin

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

F.J.K.

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

P.J. Plauger

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
 
F

F.J.K.

P.J. Plauger said:
[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?
 
F

F.J.K.

P.J. Plauger said:
[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?
 
J

Jerry Coffin

[ ... ]
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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top