string not working in c++

G

Gaurav

Hello,

I am using visual c++ 6 and i am having problems with string to work.

******** Here is the program project.cpp*********
#include <iostream.h>
#include <string>

#include "stdafx.h"

// This program just inverts the tickers.csv files execpt first line
int main(){

string ticker ;

return 0;
}
***************************

on compiling i get this error message
**************************
Compiling...
StdAfx.cpp
Compiling...
Project.cpp
C:\Windows\Desktop\Project\Project.cpp(17) : error C2065: 'string' :
undeclared identifier
C:\Windows\Desktop\Project\Project.cpp(17) : error C2146: syntax error
: missing ';' before identifier 'ticker'
C:\Windows\Desktop\Project\Project.cpp(17) : error C2065: 'ticker' :
undeclared identifier
Error executing cl.exe.

Project.exe - 3 error(s), 0 warning(s)
****************************

can someone tell me whats wrong here ?

thank you in advance.

Gaurav
 
M

Mike Wahler

Gaurav said:
Hello,

I am using visual c++ 6 and i am having problems with string to work.

******** Here is the program project.cpp*********
#include <iostream.h>

No such header in standard C++. The header which
declares the standard streams is <iostream> (no .h)
You're not using anything from it anyway, so you
can simply omit this line.
#include <string>

#include "stdafx.h"

A Microsoft specific header, not part of standard C++.
The code you've posted doesn't need it anyway, so
you can omit this line.
// This program just inverts the tickers.csv files execpt first line

Looks to me like all it does (tries to do) is define a string
object, then terminate.
int main(){

string ticker ;

std::string ticker;
return 0;
}

Applying changes I point out, we get:

#include <string>

int main()
{
std::string ticker;
return 0;
}

which will successfully compile without a diagnostic
on a compliant implementation, as well as with VC++ v6.0

***************************

on compiling i get this error message
**************************
Compiling...
StdAfx.cpp
Compiling...
Project.cpp
C:\Windows\Desktop\Project\Project.cpp(17) : error C2065: 'string' :
undeclared identifier

Accurate diagnostic message.
C:\Windows\Desktop\Project\Project.cpp(17) : error C2146: syntax error
: missing ';' before identifier 'ticker'

An artifact of the first message.
C:\Windows\Desktop\Project\Project.cpp(17) : error C2065: 'ticker' :
undeclared identifier

An artifact of the first message.
Error executing cl.exe.

Yup. :)
Project.exe - 3 error(s), 0 warning(s)
****************************

can someone tell me whats wrong here ?

See above.

-Mike
 
G

Gaurav

Mike Wahler said:
No such header in standard C++. The header which
declares the standard streams is <iostream> (no .h)
You're not using anything from it anyway, so you
can simply omit this line.


A Microsoft specific header, not part of standard C++.
The code you've posted doesn't need it anyway, so
you can omit this line.


Looks to me like all it does (tries to do) is define a string
object, then terminate.


std::string ticker;


Applying changes I point out, we get:

#include <string>

int main()
{
std::string ticker;
return 0;
}

which will successfully compile without a diagnostic
on a compliant implementation, as well as with VC++ v6.0



Accurate diagnostic message.


An artifact of the first message.


An artifact of the first message.


Yup. :)


See above.

-Mike

********************
thanx

It works fine now. i have another problem using vector<string>



#include "stdafx.h"
#include <fstream>
//#include <ofstream.h>
#include <string>
#include <vector>

using namespace std;

// This program just inverts the tickers.csv files execpt first line
int main(){


//string ticker, line;
//string input,output;
//ifstream Tickers( "tickers.txt", ios::in);
//ifstream Input_File;
//ofstream Output_File;
vector<string> temp;
/*
while(Tickers>>ticker){
input = "tempdata/" + ticker + ".csv";
output = "tempdata/" + ticker + "1.csv";
Input_File.open( input.c_str(), ios::in);
Output_File.open ( output.c_str(), ios::app);
while ( Input_File >> line ){
temp.push_back(line);
}
Output_File << temp[0] << endl;
for ( unsigned i = ( temp.size() - 1 ); i > 0; i--)
Output_File << temp << endl;
temp.clear();
Input_File.close();
Output_File.close();

}
*/

return 0;
}


Here effectively i just have vector<string> temp in main, everything
else is commented.

If i compile this program i get following warnings

*********************
Compiling...
project.cpp
C:\Windows\Desktop\project\project.cpp(43) : warning C4786:
'std::reverse_iterator said:
const *,std::basic_string<char,std::char_traits<char>,std::allocator<char>
,std::basic_string<ch
ar,std::char_traits<char>,std::allocator<char> > const
&,std::basic_string<char,std::char_traits<char>,std::allocator<char> >
const *,int>' : identifier was truncated to '255' characters in the
debug information
C:\Windows\Desktop\project\project.cpp(43) : warning C4786:
'std::reverse_iterator said:
*,std::basic_string<char,std::char_traits<char>,std::allocator<char>
,std::basic_string<char,std
::char_traits<char>,std::allocator<char> >
&,std::basic_string<char,std::char_traits<char>,std::allocator<char> >
*,int>' : identifier was truncated to '255' characters in the debug
information
c:\program files\microsoft visual studio\vc98\include\vector(39) :
information
c:\program files\microsoft visual studio\vc98\include\vector(60) :
information
Linking...

project.exe - 0 error(s), 4 warning(s)
***********************

how can i get rid of these ?

Also if i remove all the comments, i have problems with temp.clear()

All this program is doing is inverting about 40 files, whose names are
in tickers.txt.

thank you in advance.

Gaurav
 
H

hongky

in the top of the codes
u can do it as following, bcoz not namespace:):
....
#include <string>
....
using namespace std;

void fun()
{
std::string str;
}
 
M

Mark Warren

Gaurav said:
It works fine now. i have another problem using vector<string>



#include "stdafx.h"
#include <fstream>
//#include <ofstream.h>
#include <string>
#include <vector>

using namespace std;

// This program just inverts the tickers.csv files execpt first line
int main(){


//string ticker, line;
//string input,output;
//ifstream Tickers( "tickers.txt", ios::in);
//ifstream Input_File;
//ofstream Output_File;
vector<string> temp;
/*
while(Tickers>>ticker){
input = "tempdata/" + ticker + ".csv";
output = "tempdata/" + ticker + "1.csv";
Input_File.open( input.c_str(), ios::in);
Output_File.open ( output.c_str(), ios::app);
while ( Input_File >> line ){
temp.push_back(line);
}
Output_File << temp[0] << endl;
for ( unsigned i = ( temp.size() - 1 ); i > 0; i--)
Output_File << temp << endl;
temp.clear();
Input_File.close();
Output_File.close();

}
*/

return 0;
}


Here effectively i just have vector<string> temp in main, everything
else is commented.

If i compile this program i get following warnings

*********************
Compiling...
project.cpp
C:\Windows\Desktop\project\project.cpp(43) : warning C4786:
,std::basic_string<ch
ar,std::char_traits<char>,std::allocator<char> > const
&,std::basic_string<char,std::char_traits<char>,std::allocator<char> >
const *,int>' : identifier was truncated to '255' characters in the
debug information
C:\Windows\Desktop\project\project.cpp(43) : warning C4786:
'std::reverse_iterator said:
*,std::basic_string<char,std::char_traits<char>,std::allocator<char>
,std::basic_string<char,std
::char_traits<char>,std::allocator<char> >
&,std::basic_string<char,std::char_traits<char>,std::allocator<char> >
*,int>' : identifier was truncated to '255' characters in the debug
information
c:\program files\microsoft visual studio\vc98\include\vector(39) :
warning C4786:
'std::vector said:
,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocato

::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char>

,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocato
information
c:\program files\microsoft visual studio\vc98\include\vector(60) :
warning C4786:
'std::vector said:
,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocato

::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char


,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocato
information
Linking...

project.exe - 0 error(s), 4 warning(s)
***********************

how can i get rid of these ?


These can be safely ignored. If you want to hide them look in the MS
documentation for the #pragma method to do this. #pragma is not part of
standard C++, so any detailed questions about this should be directed to a
MS specific newsgroup.
Also if i remove all the comments, i have problems with temp.clear()

What problems exactly.
 
C

Chris

If i compile this program i get following warnings
*********************
Compiling...
project.cpp
C:\Windows\Desktop\project\project.cpp(43) : warning C4786:
'std::reverse_iterator said:
const *,std::basic_string<char,std::char_traits<char>,std::allocator<char>
,std::basic_string<ch
ar,std::char_traits<char>,std::allocator<char> > const
&,std::basic_string<char,std::char_traits<char>,std::allocator<char> >
const *,int>' : identifier was truncated to '255' characters in the
debug information
C:\Windows\Desktop\project\project.cpp(43) : warning C4786:
'std::reverse_iterator said:
*,std::basic_string<char,std::char_traits<char>,std::allocator<char>
,std::basic_string<char,std
::char_traits<char>,std::allocator<char> >
&,std::basic_string<char,std::char_traits<char>,std::allocator<char> >
*,int>' : identifier was truncated to '255' characters in the debug
information

[...snip...]

how can i get rid of these ?

Also if i remove all the comments, i have problems with temp.clear()

All this program is doing is inverting about 40 files, whose names are
in tickers.txt.

thank you in advance.

Gaurav

This is a problem with VC. You need to include

#pragma warning(disable: 4786)

at the top of the relevant header files. If there are many then do it in stdafx.h

Cheers,

Chris
 

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,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top