Using the std namespace in user written classes in a Windows Form project.


I looked around and haven't found an answer to this issue yet. Basically, I'm working with a simple Windows Form project in Visual Studio (with the usual setup files like Form1.h, stdafx.h and so on). I named the project App1, and it is simply supposed to create a window with a button and some numeric fields; I enter numbers into the fields, click the button and an answer comes out. This answer is calculated using a formula inside a class I wrote, (named Trinomial.h for possible future reference, it calculates option prices using a trinomial tree) so I am trying to include this class in the project as well.

Now, the issue is that I need to use the std namespace in FMC.h, which I solve using the 'using namespace std' in the beginning, however the compiler complains with the following:

1>------ Build started: Project: App1, Configuration: Debug Win32 ------
1> App1.cpp
1>App1.cpp(9): error C2976: 'std::tr1::array' : too few template arguments
1> c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\array(18) : see declaration of 'std::tr1::array'
1>App1.cpp(9): error C3699: '^' : cannot use this indirection on type 'std::tr1::array'
1> compiler replacing '^' with '*' to continue parsing
1>App1.cpp(9): error C2664: 'main' : cannot convert parameter 1 from 'int' to 'std::tr1::array *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I know this has to do with the std namespace inclusion because when I comment it out the error disappears (but the class doesn't work as a result).

I am wondering whether someone here is familiar with the reason this happens. I can include the code as well if anyone wants, but it isn't anything beyond the standard windows form setup and this one extra class I'm trying to include.

Thank you.
It might be helpful to include the code as well. You seem to not be using std::tr1::array as it is defined.

You don't have to set your working namespace to std like using namespace std; to create the array, you can create it by referring to the namespace explicitly upon the array's creation like:
std::tr1::array<int, 3> myArray;

Also, I haven't used std::tr1::array, but I note that the array template is defined in the std::tr1 namespace, not simply in the std namespace.

Last edited on
You're right, I could just append std:: everywhere, but I think the code looks ugly that way, and so I was wondering whether there was a way to make std the working namespace.

I've been trying to figure this out for hours now, and it now seems it wasn't the std namespace that was causing the problems, but the fact that I included <array> in the header file. More specifically, I started a new windows form project, and included the following header file

#include <iostream>
#include "StdAfx.h"
#include <vector>
//#include <array>

using namespace std;

class classthis
	classthis(int input);
	int membervariable1;
	vector<double> tree;

classthis::classthis(int input)
	std::cout<<"classthis constructed";
	membervariable1 = input;


I add #include "classthis.h" in the StdAfx.h file, run it, and everything works fine. However, when I de-comment the #include <array> I once again get the error message above. All the other code involved is just the standard windows forms setup in visual c++ 2010.

It's like the main function has a problem with simply including <array> even though I'm not even using it (and <vector> works fine!). I'm sure there is an obvious answer to this, I just don't know what it is.
Do you understand what you are doing when you use using namespace std; in C++?

The using directive, ie using namespace std;, does not add a name to the current scope, it only makes the names accessible from it. This means that:
» If a name is declared within a local scope it hides the name from the namespace;
» a name in a namespace hides the same name from an enclosing scope;
» a compilation error occurs if the same name is made visible from multiple namespaces or a name is made visible that hides a name in the global name space.

using Declarations, ie:
using std::cout;
using std::cin;
using std::endl

add names to the scope in which they are declared. The effect of this is:
» a compilation error occurs if the same name is declared elsewhere in the same scope;
» if the same name is declared in an enclosing scope, the the name in the namespace hides it.

So, in using the using directive for your C++ standard library stuff are causing the compiler no end of grief for the .net namespaces.
Thanks for the answers. I am simply using vectors now instead of arrays, and so the compiler complaint above is no longer present. As you indicated, the namespace inclusion didn't cause the issue.

Interestingly, I am now getting an "out of bounds" error for one of the vector manipulating formulas in one of the classes I included in the windows forms project, despite the fact that the class works fine when I run it in a simple, empty project of its own. So that is my current problem, hopefully I'll manage to figure that out soon, if not I'll probably post a new topic on that in this forum.

All in all the interaction between C++ and the .Net stuff seems quite convoluted (to my beginner eyes at least).
Topic archived. No new replies allowed.