I created a .h file and including it breaks my windows form

Jun 20, 2018 at 12:49am
I'm working on exception handling in a windows form app made in Visual Studios 2015.

The program runs fine, minus the exceptions that I'm working on, so I created an exception class in a .h file. when I write the #Include statement in the the .cpp I get a problem with the main().

1
2
3
4
5
6
7
8
9
[STAThread]
void Main(array<String^>^ args)
{
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false);

	RocketTrajectorySimulator::MyForm form;
	Application::Run(%form);
}


I get error codes C2976 "'std::Array' too few template arguments" and C3699 "'^' cannot use this indirection on type 'std::Array'".

On the line with void Main(array<String^>^ args)the > symbol has the red squiggly underscore.

As soon as I remove the .h file this goes away and everything is back to normal. Why does adding my own .h file break this and how do I fix it?

Thanks.
Last edited on Jun 20, 2018 at 12:52am
Jun 20, 2018 at 1:55am
Does the header have a using namespace std; statement?
Jun 20, 2018 at 1:55am
This site is more about standard C++ than "managed" C++ (or C++/CLI or whatever it's called).

However, as a guess it may be getting confused between std::array and cli::array. Are you saying "using namespace std" in your header? If so, remove it and put the std:: everywhere it's needed in that file. You should never say "using namespace std" in a header file, anyway.

But it's weird that "Array" is capitalized.
Last edited on Jun 20, 2018 at 1:58am
Jun 20, 2018 at 7:04am
Since you don't use the commandline args you can just remove them.
void Main()
If you need them later you can get them with Environment::GetCommandLineArgs()
Jun 20, 2018 at 11:12am
Thanks guys! removing using namespace std; from the header fixed it.
Jun 20, 2018 at 12:24pm
As a general rule, don't ever put "using" statements in headers. Especially not using namespace std;
Jun 22, 2018 at 9:07pm
What is the actual reason that it's bad to put a using statement in a header? This is the first time I've ever had an issue with it and it seems that it is mostly because c++/CLR is different somehow than standard c++..
Jun 22, 2018 at 10:25pm
The reason is exactly what you've seen here. You're dumping an entire namespace into the global namespace without any regard for possible collisions. The more libraries your program uses the more possibilities there are that you will stumble upon a collision, especially with such common names the standard library uses, such as vector, set, sort, etc.
Personally, I never use using namespace std at all. It's not such a big deal to type out the std:: in front of the members and it makes the code more explicit. If you're going to do it, prefer to do it in local scope. E.g.:
1
2
3
4
void foo(){
    using namespace std;
    cout << "hello\n";
}
Jun 22, 2018 at 11:58pm
You can also insert individual names into the global (or better yet, function level) namespace:
1
2
3
4
5
6
int main() {
    using std::cout; //we're using cout a lot here, so remove the std:: eyesore
    ...
    cout << "Yo\n";
    ...
}

I only use "using namespace std" at global scope for short example code. Maybe I should stop doing that.
Jun 23, 2018 at 11:32am
Thanks, helios and tpd. That's really interesting. I'll keep this all in mind for the future. I appreciate it!
Topic archived. No new replies allowed.