Using CLR in an empty vc++ project

I have been using Ivor Horton's Beginning Visual C++ Programming and have had no problems until now.

All examples have worked until now, the project is setup as a Win32 Console Application with the Application Settings set to Empty Project.

What do I need to do so the vc++ compiler will find the proper include files.

When I try to compile the Ex4_13.cpp file I get an error stating that "stdafx.h" cannot be found.

I have tried to set the /clr compile option as well with the same results.

I have also tried to use the CLR template but that creates more problems.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Ex4_13.cpp : main project file.
// Using a CLR array
#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
  array<double>^ samples = gcnew array<double>(50);

  // Generate random element values
  Random^ generator = gcnew Random;
  for(int i = 0 ; i< samples->Length ; i++)
    samples[i] = 100.0*generator->NextDouble();

  // Output the samples
  Console::WriteLine(L"The array contains the following values:");
  for(int i = 0 ; i< samples->Length ; i++)
  {
    Console::Write(L"{0,10:F2}", samples[i]);
    if((i+1)%5 == 0)
      Console::WriteLine();
  }

  // Find the maximum value
  double max = 0;
  for each(double sample in samples)
    if(max < sample)
      max = sample;

  Console::WriteLine(L"The maximum value in the array is {0:F2}", max);
  return 0;
}
stdafx.h is a user header file that is (should be?) automatically created by VC++ and simply contains a list of the other header files you have created - so you only need a singl #include in your .cpp files rather than a list of them.
It also prcompiles the headers, which I belive is an optimisation to shorten compile time (others may correct me on this).
Since you are just writing a simple prog and not using any headers you should be able to just remove the line from the example and will all work.
Topic archived. No new replies allowed.