Computer Science Canada

Microsoft Visual Studio .Net 2003

Author:  Andy [ Thu Feb 24, 2005 10:11 pm ]
Post subject:  Microsoft Visual Studio .Net 2003

hmm i recently acquired a copy of vsn2003 and i was very confused by the ide... i've been a vc6 user for quite some time.. i was just wondering if anyone knew what #include "stdafx.h" is for.. it seems that if i dont have that my programs in console app wont work...

Author:  wtd [ Thu Feb 24, 2005 10:34 pm ]
Post subject: 

Show me your code. Anything console based should be easily possible in standard C++.

Author:  Andy [ Thu Feb 24, 2005 10:36 pm ]
Post subject: 

i was simply doing some vector stuff
c++:

// vector_push_back.cpp
// compile with: /EHsc
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;   

int main( )
{
   vector <int> v1,v2;
   v2.push_back(6);
   v2.push_back(7);
   v1.push_back(1);
   v1.push_back(3);
   v1.push_back(2);
   v1.push_back( 0);
   v1.push_back(5);
   v1.push_back(4);
   
   v1.insert(v1.begin(),v2.begin(),v2.end());

   //sort(v1.begin(),v1.end());
   v1.erase(v1.begin()+3,v1.begin()+4);

   for (int i=0;i<v1.size();i++)
       cout<<v1[i];
 }


and when i took off the #include "stdafx.h", it wont compile and gives me an error saying
code:
c:\Documents and Settings\Owner.SUN\My Documents\Visual Studio Projects\ccc practice\ccc practice.cpp(29) : fatal error C1010: unexpected end of file while looking for precompiled header directive

Author:  wtd [ Thu Feb 24, 2005 10:41 pm ]
Post subject: 

Works fine with GCC, so I call bull$@!# on Microsoft's part.

Author:  Mazer [ Fri Feb 25, 2005 8:32 am ]
Post subject: 

Could it be the project settings? I noticed that when some people create a new project it would add a bunch of crap to their main cpp file if they used a certain option. Maybe if you just choose an empty win32 console application?

Author:  bugzpodder [ Fri Feb 25, 2005 1:10 pm ]
Post subject: 

Andy wrote:
i was simply doing some vector stuff
c++:

// vector_push_back.cpp
// compile with: /EHsc
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;   

int main( )
{
   vector <int> v1,v2;
   v2.push_back(6);
   v2.push_back(7);
   v1.push_back(1);
   v1.push_back(3);
   v1.push_back(2);
   v1.push_back( 0);
   v1.push_back(5);
   v1.push_back(4);
   
   v1.insert(v1.begin(),v2.begin(),v2.end());

   //sort(v1.begin(),v1.end());
   v1.erase(v1.begin()+3,v1.begin()+4);

   for (int i=0;i<v1.size();i++)
       cout<<v1[i];
 }


and when i took off the #include "stdafx.h", it wont compile and gives me an error saying
code:
c:\Documents and Settings\Owner.SUN\My Documents\Visual Studio Projects\ccc practice\ccc practice.cpp(29) : fatal error C1010: unexpected end of file while looking for precompiled header directive


what i would do is open a fresh C++ file (dont open any projects) - ie a file with .cpp extension. and paste your code in there and build/compile. try to take out the stdafx.h while doing that. also, instead of declaring int i=0 in the for loop, maybe take it out will help. attach a return 0; in the end.

Author:  wtd [ Fri Feb 25, 2005 3:48 pm ]
Post subject: 

bugzpodder wrote:
also, instead of declaring int i=0 in the for loop, maybe take it out will help. attach a return 0; in the end.


The declaration of i in the loop is fine, though you should use iterators. And yes, you do need to return 0, but it will work without that, and that isn't what the error in this case is about.

With iterator:

code:
for (vector<int>::iterator i(v1.begin()); i != i.end(); i++)
   cout << *i;


Or use for_each.

code:
#include <algorithm>

...

void print_int(int i) { cout << i; }

...

for_each(v1.begin(), v1.end(), print_int);

Author:  wtd [ Fri Feb 25, 2005 6:35 pm ]
Post subject: 

Or more flexibly...

code:
#include <algorithm>

...

class print
{
   private:
      ostream& out;
   public:
      print(ostream& init_out) : out(init_out) {}
      void operator()(int i) { out << i; }
};

...

for_each(v1.begin(), v1.end(), print(cout));

Author:  Andy [ Fri Feb 25, 2005 10:10 pm ]
Post subject: 

wow wtd! thx! question tho... for the sort function under the algorithm class... how can i make it sort from greatest to least? does it have something to do with the overloaded third parameter?

Author:  wtd [ Fri Feb 25, 2005 10:40 pm ]
Post subject: 

Yes. The third argument is a binary predicate. This is either a function or function object which takes two arguments and returns a boolean value. For an ascending sort, the comparison is less than. To reverse it, we simply use greater than. So, we need a binary predicate:

code:
template <typename T>
bool desc(T a, T b)
{
        return a > b;
}


And then to sort:

code:
sort(v1.begin(), v1.end(), desc<int>);

Author:  Andy [ Sat Feb 26, 2005 1:32 pm ]
Post subject: 

bugzpodder wrote:

what i would do is open a fresh C++ file (dont open any projects) - ie a file with .cpp extension. and paste your code in there and build/compile. try to take out the stdafx.h while doing that. also, instead of declaring int i=0 in the for loop, maybe take it out will help. attach a return 0; in the end.


i tried that... simply opening up vc7 and select new file, then c++ source, but when i press ctrl f5, nothing happens...

Author:  Andy [ Sat Feb 26, 2005 2:42 pm ]
Post subject: 

wtd wrote:

code:
for (vector<int>::iterator i(v1.begin()); i != i.end(); i++)
   cout << *i;



dont u mean v1.end()?

Author:  wtd [ Sat Feb 26, 2005 2:53 pm ]
Post subject: 

Andy wrote:
wtd wrote:

code:
for (vector<int>::iterator i(v1.begin()); i != i.end(); i++)
   cout << *i;



dont u mean v1.end()?


Err... yes.

Author:  md [ Sat Feb 26, 2005 6:56 pm ]
Post subject: 

stdafx.h and the related cpp file are the precomiled header stuff that VCC creates for you. I just delete them and change the project options to not use precompiled headers; project options->C++ (or code not sure exaclty as i'm not at my normal computer...)->Precompiled headers. If you just remove the files and/or not include them without changing the options then MSVC gives the "unexpected end of file" error. So far I've had no adverse problems with disabling precompiled headers other than a slightly longer build time.

Author:  Andy [ Sun Feb 27, 2005 3:15 pm ]
Post subject: 

YESYESYESYESYES!!!! thank you sooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo much... btw, now i have another problem... after i disabled that thing, when ever i start an empty console application, i have to make a new source file, save it in the folder, then move it into the project... does anyone know an easier way?

Author:  md [ Sun Feb 27, 2005 6:52 pm ]
Post subject: 

solution explorer->right click on the source files folder->add new item; select the cpp file, and voila.

I prefer using the solution explorer view over the class view because I usually put each class or group of very similar classes in one file, so just having a list of files is makes it much easier to edit code.

Author:  Andy [ Sun Feb 27, 2005 8:22 pm ]
Post subject: 

wow.. i've been looking all over for that solution explorer.. thanks so much

Author:  bugzpodder [ Sun Feb 27, 2005 8:44 pm ]
Post subject: 

alternatively, you could use reverse(v.begin(),v.end()); to reverse the list after you sort it in ascending order.

Author:  Andy [ Sun Feb 27, 2005 9:06 pm ]
Post subject: 

wow.. that is awesome.. thx bugz +20 bits

Author:  wtd [ Sun Feb 27, 2005 10:52 pm ]
Post subject: 

Andy wrote:
YESYESYESYESYES!!!! thank you sooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo much... btw, now i have another problem... after i disabled that thing, when ever i start an empty console application, i have to make a new source file, save it in the folder, then move it into the project... does anyone know an easier way?


Use GCC and learn standard C++?

No, seriously, you're learning how to be Microsoft's b****, not how to write good, standard C++ programs.

Author:  Andy [ Mon Feb 28, 2005 8:18 am ]
Post subject: 

once i get my laptop, i think im gona lookin GCC more since i'll be running an ubuntu/xp dualboot... i guess there really is nothing wrong with learning msvc... plus, the compiler is alot better than the vc6 one...

Author:  Mazer [ Mon Feb 28, 2005 8:21 am ]
Post subject: 

I find it easier to work with a nice and simple (preferably tabbed) text editor and using makefiles.


: