MS Vis Studio "not a type"..??? error

Pages: 12
I am new to using MS VS 2015 - which I acquired for getting into my C++ studies and training at the moment. Long story short==> I made a simple class in a header and defined some very simple constructors as well as some "get" functions to return private member data and a helper function.

I haven't used VS 2015 at all before so I am not sure what I am doing wrong as a warning to all reading this!

I have created a namespace for the class inside the header file that defined the class and I have this namespace inside the definition source code as well (I am following a book example BTW) and I have NO ERRORS in either the header file or the source code definitions file.

However in the MAIN routine source file is where things get odd (even though I have included the header file for the class definition in the main source file.)

I instantiated an object of my class (see code snippet below)==>

 
  My_Class my_class_obj{ //some stuff for the overloaded constructor } 


and I get an error from the compiler saying that the "My_class" identifier used to identify the new object's type is NOT a type! I sure as heck thought it was since that is the idea behind a user defined type??

So I am figuring (correctly?) that there is a minor or possibly a major problem going on here with my ignorance regarding what VisStudio expects or needs to instantiate a newly designed class object.

Any Ideas out there VS users? You probably will think this is trivial (and it probably is) but for someone new to the VS IDE..... not sure what is going on here.

Xanadu

Is it "My_Class" (your code) or "My_class" (your quote)? C++ is case sensitive after all.

If that isn't the issue, then post your code so we can see, otherwise I think we'd just be guessing as to what the exact issue is.
VS 2015 might not support using {} for initialization. Try using My_Class my_class_obj(/*params*/);
Good thought, but VS 2015 supports initializer lists ("list-initialization").
https://docs.microsoft.com/en-us/previous-versions/hh567368(v=vs.140)

1
2
3
4
5
6
7
8
9
10
11
12
class My_Class {
public:
	My_Class(int x, int y) : x(x), y(y) { }
	
	int x;
	int y;
};

int main()
{
	My_Class my_class_obj{ 3, 2 };
}

Compiles fine for me.
Last edited on
I have created a namespace for the class inside the header file that defined the class and I have this namespace inside the definition source code as well (I am following a book example BTW) and I have NO ERRORS in either the header file or the source code definitions file.


I'm guessing this is the source of the problem. Your main() function will be in the global namespace, not the namespace your class is in. So you'll need to use the namespace to qualify your class type:

My_Namespace::My_Class my_class_obj{ 3, 2 };
Ok,

I might have to post the code but you guys have given some possible issues...

1.) I DID use {}'s when defining the list initialization and NOT ()'s around these... so I am hearing that VS does NOT like that? Do I need to change these to regular parentheses?

2.) I tried fully qualifying the instantiation WITH the namespace as Mikey recommended and that did nothing to help that error.

3.) I will look at the case sensitivity but I think I'm good on that point.

Thank you guys. I will go and look at all of these points and get back with you regardless of the outcome.

Xanadu
I might have to post the code

Until you do, we're just playing guessing games.
Fair enough......

If I get a moment I will post it later today. Maybe a couple hours since that code is at "home" and I am not....... but I'll see what I can do.

Xanadu
@Xanadu4ever,

I think other posts already have you covered...

I wanted to address the subject and point that you're exploring Visual Studio.

From a language viewpoint there should not be anything unique to Visual Studio compared to any other compiler, given the fact that all compilers have minor differences in language support specifics. All compilers generally implement the C++ language spec the same way (or the rest of the public of programmers complain loudly), what differs is how much of a particular recent standard has been implemented.

Visual Studio is merely an IDE packaged with Microsoft's compiler. IMO it's an excellent IDE (to the point I wish it ran on Linux). The compiler, on the other hand, while good, is known to be behind in how much of the most recent C++ language specs are implemented. It's good through C++14.

Is there a reason you're not using VS 2019 or VS 2017? The CE versions are free, and they are full featured, so there's hardly a reason not to update.

I haven't used VS 2015 for a while now, but from VS 2017 forward you can easily (by merely clicking the checkbox) include the CLang/LLVM compiler in the mix.

CLang/LLVM is usually more up to date for any new language specs than Microsoft's compiler (barely behind GCC).

Using CLang/LLVM is almost no different than the built in MS compiler. This gives VS users two compilers we can configure in our projects for dual build/check for portability of code among the various compilers, with hardly any effort installing or configuring for it.

Anyway, VS 2015 through VS 2019 work and look very similarly, so an upgrade won't be much of a shock, just better compiler/language support.

That said, there shouldn't be anything you're writing which has specific differences or "gotchas" just because it's in Visual Studio instead of some other compiler/IDE. That's really about language level (C++11/C++14), and there's a compiler configuration setting for that.

Niccolo,

Thanks for the heads up about VS 2015 and the only reason I have that is because it is the full Pro version that was given to me by someone who purchased the full thing.

Now with that said, you're right, I could just go all the way to 2019 and get CLang/LLVM as an additional compiler. Since you're indicating that semantics should NOT be a problem, I will post the code here in a few minutes so everyone can see and perhaps I'm just doing something stupid (more than likely...) or forgot a minor item which is throwing everything into a tail spin....

stand by for code......

Xanadu
OK, Here's the header file==>

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <string>
#include <iostream>
#include <exception>
#include <stdbool.h>
#include <vector>
#include <stdio.h>


namespace Book {	//namespace of the book declaration

	class book
	{

	public:

		class invalid {};	//for errors found in book definition

		//constructors
		book(std::string isbn, std::string title, std::string author, int cpyr);
		book();			//default constructor

		~book();	//default destructor

		//non-modifying operations
		std::string get_ISBN() const { return ISBN; }
		std::string get_title() const { return title; }
		std::string get_author() const { return author; }
		int get_cpyrite() const { return cpyrite; }
		bool get_chk_status() const { return checked; }
		
		//modifying operations
		void check_out() { checked = true; }
		void check_in() { checked = false; }


	private:

		std::string ISBN;	//ISBN number of the book
		std::string title;
		std::string author;
		int cpyrite;		//copyright date
		bool checked{false};	//status of whether the book is checked out

	};//end class book definition

	//helping functions

	bool is_book(std::string isbn, std::string title, std::string author, int cpyrite);

	

}//end Book namespace 

Here's the source code definitions==>

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "Book_class.h"

namespace Book {

	book::book(std::string ISBN, std::string title, std::string author, int cpyr) {

		if (is_book(ISBN, title, author, cpyr) == false) //throw invalid{};

			std::cout << "Error in constructing a book!";

	}//overloaded constructor


	const book& default_book() {

		static book bb {"0-0-0-Z", "default", "default", 1900};
		return bb;
	}//end default book definition

	book::book()
		:ISBN{default_book().get_ISBN()},
		title{default_book().get_title()},
		author{default_book().get_author()},
		cpyrite{default_book().get_cpyrite()},
		checked{default_book().get_chk_status()}

	{}	//end default constructor

	bool is_book(std::string isbn, std::string title, std::string author, int cpyrite) {

		if (cpyrite <= 0) return false;		//if copyright year is negative or zero
		if (author.empty() ) return false;	//if author string is empty
		if (title.empty() ) return false;	//if title string is empty

		char ch;

		for (unsigned i : isbn)
		{
			ch = isbn.at(i);

			if (i == 1 || i == 3 || i == 5 && ch != '-') {
				return false;				//if every odd position isn't a dash return false
			}
			else if (i == 0 || i == 2 || i == 4 && !isalpha(ch)) {
				return false;				//if every even position up to the 6th element isn't a digit return false
			}
			else if (i == 6 && !isalnum(ch)) {
				return false;				//if the last character is not an alpha numeric return false
			}//end if=else

		}//end for block

		return true;	//all formatting is good and we have a legitimate book def.

	}//end of is_book definition
			
	
}//end namespace Book 
Here's the Main routine==>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Book_Class.h"	//user defined interface


int main(){

	book my_book{"5-6-3-h", "The ways of the force", "Yoda", 1983};

	std::cout <<"The ISBN of your book is: "<<my_book.get_ISBN()<<"\n";
	std::cout << "The title of your book is: " << my_book.get_title() << "\n";
	std::cout << "The author of your book is : " << my_book.get_author() << "\n";
	std::cout << "The copyright year of your book is: " << my_book.get_cpyrite() << "\n";
	std::cout << "The checked out status of your book is: " << my_book.get_chk_status() << "\n";
	


	return 0;
}   //end of print_nums function 
1. Book::book my_book{"5-6-3-h", "The ways of the force", "Yoda", 1983};
(edit: As MikeyBoy said)

2. You need to implement your destructor, since you defined it in your class definition.
Alternatively, just don't define it (delete header file line 22) and let the compiler implement it.
Last edited on
So far as I see, the only thing stopping compilation is the namespace.

In main, first line, Book::book.

That's it so far as I can see.

Edit:

This is an example of illustrating what one thinks is happening as opposed to posting the actual code.

This wasn't some theoretical issue. It was just a name thing, a minor detail that's quick to notice once one (out here) can see the actual code.

It's an important point when asking questions: The compiler is extremely literal.
Last edited on
I think you need to read @MikeyBoy's earlier post.
Very well gentlemen,

I will fully qualify that instantiation and get rid of the destructor for now. I'll let you know what happens either way....

Xanadu
Ok people,

Again, you were all correct...…. full scoping that instantiation AND commenting out the destructor allowed full compilation with no errors and no warnings.

Thanks again!

Next problem... when I try to run it...… it says that there was an Error and "Abort()" was called. Then it asked if I want to run in debug mode.


Niccolo, are these those compiler settings you talked about earlier coming home to roost? Do I have to set something up in this IDE still to make it behave??


Xanadu
Probably not.

If you built and ran the program in release mode, then switch to debug mode.

Inside the IDE, if you're building in debug mode, hit F5 or choose to start debugging from the debug menu.

What I saw was simple.

In is_book, in the for loop (for( unsigned i: isbn), you check for isbn.at(i).

Unfortunately, i is the ch you seek anyway, it's 53, and there's no 53rd entry in isbn. That's the crash

Just make the for loop a char ch instead of an unsigned i

Edit:

oh

You want the index, too.

Use an old fashioned for loop, not the new style for loop - or, use the new style with char ch as the loop type, but where you fashion char ch now, make the integer and set it for 0 to start off - increment i inside the loop.


Last edited on
Ok Niccolo,

Will do. Use a regular for loop. I'll see where this goes.

Xanadu
Pages: 12