Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

problem using namespace std.


  • Please log in to reply

#1
pmukund

pmukund

    New Member

  • Member
  • Pip
  • 8 posts
Hi!
I have downloaded the project SimpleImage from msdn. I want to make a few changes to it. This is my code for the ChildView.cpp class:

#include "stdafx.h"
#include "CImage.h"
#include "ChildView.h"
#include "InfoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//#using <mscorlib.dll>
////I Pasted//
#include <CString>
#include <afx.h>
//#include <string>
//#include <string.h>
//
#include "stdafx.h"
//#ifdef OLD_HEADER_FILENAME
//#include <iostream.h>
//#else
//#include <iostream.h>
//#endif

//
//


const int NO_FILES= 3;
const int FSPACE_DIM1 = 1091*NO_FILES; // Dimension sizes of the dataset as it is...
const int FSPACE_DIM2 = 4; // ...stored in the file
//
//#include<fstream>
//using std::fstream;
using namespace std;
using namespace std::ifstream;
//using namespace std::ofstream;
//using namespace std{
// using::fstream}
//using namespace std::string;
//#include <iomanip>


//using namespace System;
//I pasted//
// CChildView

CChildView::CChildView()
{
m_nImageSize = SIZE_NONE;
}

CChildView::~CChildView()
{
}


BEGIN_MESSAGE_MAP(CChildView, CWnd)
ON_WM_PAINT()
ON_COMMAND(ID_FILE_OPENIMAGE, OnFileOpenimage)
ON_COMMAND(ID_FILE_SAVEIMAGE, OnFileSaveImage)
ON_COMMAND(ID_TOOLS_MAKEBW, OnToolsMakeBW)
ON_COMMAND(ID_FILE_IMAGEINFO, OnImageInfo)
ON_UPDATE_COMMAND_UI(ID_SIZE_HALF,OnUpdateSizeHalf)
ON_UPDATE_COMMAND_UI(ID_SIZE_ORIGINAL,OnUpdateSizeOriginal)
ON_UPDATE_COMMAND_UI(ID_SIZE_DOUBLE,OnUpdateSizeDouble)
ON_UPDATE_COMMAND_UI(ID_SIZE_FILL,OnUpdateSizeFill)
ON_UPDATE_COMMAND_UI(ID_FILE_SAVEIMAGE,OnUpdateImage)
ON_UPDATE_COMMAND_UI(ID_FILE_IMAGEINFO,OnUpdateImage)
ON_UPDATE_COMMAND_UI(ID_TOOLS_MAKEBW,OnUpdateImage)
ON_COMMAND_RANGE(ID_SIZE_HALF,ID_SIZE_FILL,OnChangeSize)
END_MESSAGE_MAP()



// CChildView message handlers

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs)
{
if (!CWnd::PreCreateWindow(cs))
return FALSE;

cs.dwExStyle |= WS_EX_CLIENTEDGE;
cs.style &= ~WS_BORDER;
cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS,
::LoadCursor(NULL, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_WINDOW+1), NULL);

return TRUE;
}

void CChildView::OnPaint()
{
CPaintDC dc(this); // device context for painting
if (!imgOriginal.IsNull())
{

switch (m_nImageSize)
{
case SIZE_HALF:
imgOriginal.StretchBlt(dc,0,0,imgOriginal.GetWidth()/2,imgOriginal.GetHeight()/2,SRCCOPY);
break;
case SIZE_ORIGINAL:
imgOriginal.StretchBlt(dc,0,0,imgOriginal.GetWidth(),imgOriginal.GetHeight(),SRCCOPY);
break;
case SIZE_DOUBLE:
imgOriginal.StretchBlt(dc,0,0,imgOriginal.GetWidth()*2,imgOriginal.GetHeight()*2,SRCCOPY);
break;
case SIZE_FILL:
CRect rctWindowSize;
GetClientRect(rctWindowSize);
imgOriginal.StretchBlt(dc,0,0,rctWindowSize.Width(),rctWindowSize.Height(),SRCCOPY);
};
}

}


void CChildView::OnFileOpenimage(void)
{
// TODO: Add your command handler code here
CString strFilter;
CSimpleArray<GUID> aguidFileTypes;
HRESULT hResult;

hResult = imgOriginal.GetExporterFilterString(strFilter,aguidFileTypes);
if (FAILED(hResult)) {
CString fmt;
fmt.Format("GetExporterFilter failed:\n%x - %s", hResult, _com_error(hResult).ErrorMessage());
::AfxMessageBox(fmt);
return;
}

CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, strFilter);
dlg.m_ofn.nFilterIndex = m_nFilterLoad;
hResult = (int)dlg.DoModal();
if(hResult != IDOK) {
return;
}

m_nFilterLoad = dlg.m_ofn.nFilterIndex;
imgOriginal.Destroy();
hResult = imgOriginal.Load(dlg.GetFileName());
if (FAILED(hResult)) {
CString fmt;
fmt.Format("Load image failed:\n%x - %s", hResult, _com_error(hResult).ErrorMessage());
::AfxMessageBox(fmt);
return;
}

m_nImageSize = SIZE_ORIGINAL;
Invalidate();
UpdateWindow();

}

void CChildView::OnFileSaveImage(void)
{
CString strFilter;
CSimpleArray<GUID> aguidFileTypes;
HRESULT hResult;


strFilter = "Bitmap image|*.bmp|JPEG image|*.jpg|GIF image|*.gif|PNG image|*.png||";

CFileDialog dlg(FALSE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_EXPLORER,strFilter);
dlg.m_ofn.nFilterIndex = m_nFilterLoad;
hResult = (int)dlg.DoModal();
if (hResult != IDOK) {
return;
}

// Add the appropriate extension if the user didn't type one

CString strFileName;
CString strExtension;

strFileName = dlg.m_ofn.lpstrFile;


// add the file extension if the user didn't supply one
if (dlg.m_ofn.nFileExtension == 0)
{
switch (dlg.m_ofn.nFilterIndex)
{
case 1:
strExtension = "bmp";
break;
case 2:
strExtension = "jpg";
break;
case 3:
strExtension = "gif";
break;
case 4:
strExtension = "png";
break;
default:
break;
}

strFileName = strFileName + '.' + strExtension;

}

// the extension on the file name will determine the file type that is saved
hResult = imgOriginal.Save(strFileName);
if (FAILED(hResult)) {
CString fmt;
fmt.Format("Save image failed:\n%x - %s", hResult, _com_error(hResult).ErrorMessage());
::AfxMessageBox(fmt);
return;
}

}

void CChildView::OnToolsMakeBW(void)
{

CWaitCursor wait;
float (*matrix)[FSPACE_DIM2] = new float[FSPACE_DIM1][FSPACE_DIM2];
float x=0;
int m=0;
int e=0;
//string y;
//CString y;
CString y;


char filename1[150] = "C:\\Documents and Settings\\ganesht-a\\Desktop\\moving to CPP\\directRAMANTOHDFALLXYforall140\\raman-files\\wy2-136";
char filename3[15]=".txt";
char filename2[15];
char filename[200];


int j=1;
for(int j=1;j<=NO_FILES;j++)
{
y = '\0';
for(int k=0;k<=200;k++)
filename[k]='\0';
for(int k=0;k<=strlen(filename2);k++)
filename2[k]='\0';

sprintf(filename2, "%d", j);

for(int i=0;i<(strlen(filename1));i++)
filename[i]=filename1[i];

m=strlen(filename);

for(int i=0;filename2[i]!='\0';i++)
filename[m+i]=filename2[i];

m=strlen(filename);

for(int i=0;filename3[i]!='\0';i++)
filename[m+i]=filename3[i];

std::ifstream inFile;
// I AM STUCK AT THE ABOVE LINE
// inFile.open(filename);
////
// if (!inFile)
// {
// cout << "Unable to open file";
// exit(1); // terminate with error
// }
//
// while(getline(inFile,y))
// if(y =="XYDATA")
// for(int i=0;i<1091;i++)
// for(int l=0;l<2;l++)
// {
// inFile >> x;
// matrix[i+(1091*(j-1))][l] = x;
// //cout<< x<<endl;
// cout<<matrix[i+(1091*(j-1))][l];
// }
//
// for(int i=0;i<j*1091;i++)
// {
// if(((i+1)%1091)==0)
// e=(i+1)/1091;
// else
// e = (i+1)/1091 + 1;
// if(e%20==0)
// matrix[i][2]=20;
// else
// matrix[i][2]=e%20;
// if(e%20==0)
// matrix[i][3]=e/20;
// else
// matrix[i][3]=(e)/20+1;
// }
//
// inFile.close();
//
}

/*if (!imgOriginal.IsIndexed()) {*/

// the image does not use an indexed palette, so we will change each pixel to B&W (slow)
COLORREF pixel;
int maxY = imgOriginal.GetHeight(), maxX = imgOriginal.GetWidth();
byte r,g,b,avg;
for (int x=0; x<maxX; x++) {
for (int y=0; y<maxY; y++) {
pixel = imgOriginal.GetPixel(x,y);
r = GetRValue(pixel);
g = GetGValue(pixel);
b = GetBValue(pixel);
avg = ((r + g + b)/3);
imgOriginal.SetPixelRGB(x,y,avg,avg,avg);
}
}

/*}*/
//else {

// // the image uses an indexed palette, so we will just change the palette table entries to
// // their B&W equivalents
// int MaxColors = imgOriginal.GetMaxColorTableEntries();
// RGBQUAD* ColorTable;
// ColorTable = new RGBQUAD[MaxColors];

// imgOriginal.GetColorTable(0,MaxColors,ColorTable);
// for (int i=0; i<MaxColors; i++)
// {
// int avg = (ColorTable[i].rgbBlue + ColorTable[i].rgbGreen + ColorTable[i].rgbRed)/3;
// /*ColorTable[i].rgbBlue = (BYTE)avg;
// ColorTable[i].rgbGreen = (BYTE)avg;
// ColorTable[i].rgbRed = (BYTE)avg;*/
// ColorTable[i].rgbBlue = i;
// ColorTable[i].rgbGreen = i;
// ColorTable[i].rgbRed = i;
//
// }
// imgOriginal.SetColorTable(0,MaxColors,ColorTable);
//
// delete(ColorTable);
//}

Invalidate();
UpdateWindow();
}


void CChildView::OnImageInfo()
{
CInfoDlg infoDlg(imgOriginal);
infoDlg.DoModal();
}


void CChildView::OnChangeSize(UINT nID)
{
m_nImageSize = (SizesEnum)(nID - ID_SIZE_BASE);
Invalidate();
UpdateWindow();
}

void CChildView::OnUpdateSizeHalf(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!imgOriginal.IsNull());
pCmdUI->SetCheck((UINT)(m_nImageSize == SIZE_HALF));
}

void CChildView::OnUpdateSizeOriginal(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!imgOriginal.IsNull());
pCmdUI->SetCheck((UINT)(m_nImageSize == SIZE_ORIGINAL));
}

void CChildView::OnUpdateSizeDouble(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!imgOriginal.IsNull());
pCmdUI->SetCheck((UINT)(m_nImageSize == SIZE_DOUBLE));
}

void CChildView::OnUpdateSizeFill(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!imgOriginal.IsNull());
pCmdUI->SetCheck((UINT)(m_nImageSize == SIZE_FILL));
}

void CChildView::OnUpdateImage(CCmdUI *pCmdUI)
{
pCmdUI->Enable(!imgOriginal.IsNull());
}




Now I could not use
#include <string> to do declare strings like
string y;
The include string statement would result in a long list of errors.
I solved this problem by changing #include <string> to #include <CString> and
string y;
to
CString y;
That solved the problem but now there is a part in which I have used the fstream class.
I cant get it to work even by giving "using namespace std::ifstream;". I get the follwwing errors:

c:\Documents and Settings\ganesht-a\Desktop\renaissance\Samples\Visual C++ .NET 2003\mfc\general\SimpleImage\ChildView.cpp(271): error C2065: 'ifstream' : undeclared identifier
c:\Documents and Settings\ganesht-a\Desktop\renaissance\Samples\Visual C++ .NET 2003\mfc\general\SimpleImage\ChildView.cpp(271): error C2065: 'inFile' : undeclared identifier
c:\Documents and Settings\ganesht-a\Desktop\renaissance\Samples\Visual C++ .NET 2003\mfc\general\SimpleImage\ChildView.cpp(271): error C2146: syntax error : missing ';' before identifier 'inFile'
c:\Documents and Settings\ganesht-a\Desktop\renaissance\Samples\Visual C++ .NET 2003\mfc\general\SimpleImage\ChildView.cpp(48): error C2871: 'ifstream' : a namespace with this name does not exist


and if i give the statement #include <fstream> , I get a big set of errors saying:

C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(30): error C2059: syntax error : ')'
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(34): error C2059: syntax error : ','
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(29): error C2078: too many initializers
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(33): error C2078: too many initializers
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(29): error C2143: syntax error : missing ';' before '('
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(34): error C2143: syntax error : missing ';' before ','
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(33): error C2143: syntax error : missing ';' before '['
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(29): error C2226: syntax error : unexpected type 'size_t'
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(29): error C2365: 'new' : redefinition; previous definition was a 'member function'
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(33): error C2365: 'new' : redefinition; previous definition was a 'member function'
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(29): error C2440: 'initializing' : cannot convert from 'int' to 'void *'
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(33): error C2440: 'initializing' : cannot convert from 'int' to 'void *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(29): error C2491: 'new' : definition of dllimport data not allowed
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(33): error C2491: 'new' : definition of dllimport data not allowed
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(33): error C3409: empty attribute block is not allowed
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xdebug(34): fatal error C1004: unexpected end of file found

in some other files.

I am a newbie and I want to knmow whether just as CString is the equivalent of string can I use something to substitute ifstream? I do not know how to use CFile .....or is there another way of making
"#include <fstream>" and "include <string>" work in this?

"#include <fstream>" and "include <string>" work fine in a console application but are not working in a MFC Application. I am confused and am not able to understand what is going on. Can some one guide me. Help would be really appreciated.

Thank you
Mukund
  • 0

Advertisements


#2
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Hi Mukund,

1. Delete the following line:
using namespace std::ifstream;
ifstream is not a namespace, it's an object type. You only use the "using namespace" statement with namespaces. The namespace in this case is "std". The line above this line is "using namespace std;" so you're already using std. Therefore, this line can be deleted.

2. You have commented out the following line of code, which needs to be there, so uncomment it:
#include<fstream>

3. You have two of the following lines of code:
#include "stdafx.h"
Your code won't work with this line duplicated. Delete the second one, and leave the first one.

4. The following two lines of code appear in seperate places in your code:
#include <CString>
...
#include<fstream>
These two lines of code need to be before any other include statements except for the stdafx.h include statement. Move them both up so that they immediately follow the #include "stdafx.h" statement, before your #include "CImage.h" statement.

After I do these 4 steps, the code builds and the app starts. Good luck from there!

-Ricci

Edited by ricci, 06 January 2006 - 02:21 PM.

  • 0

#3
pmukund

pmukund

    New Member

  • Topic Starter
  • Member
  • Pip
  • 8 posts
THat worked!! Thanks a lot Ricci ! Also I had to put the #include <fstream> above the using namespace std command...It seems the order in which we give the include statements is very important. Can you tell me how we figure out which include statement to give first..? Is it that the namespace containing the sub files should be given first? I am not very sure of the order in which to include files....

But thanks a lot for your help!!

Mukund
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP