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.
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:
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.
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.
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....
#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) returnfalse; //if copyright year is negative or zero
if (author.empty() ) returnfalse; //if author string is empty
if (title.empty() ) returnfalse; //if title string is empty
char ch;
for (unsigned i : isbn)
{
ch = isbn.at(i);
if (i == 1 || i == 3 || i == 5 && ch != '-') {
returnfalse; //if every odd position isn't a dash return false
}
elseif (i == 0 || i == 2 || i == 4 && !isalpha(ch)) {
returnfalse; //if every even position up to the 6th element isn't a digit return false
}
elseif (i == 6 && !isalnum(ch)) {
returnfalse; //if the last character is not an alpha numeric return false
}//end if=else
}//end for block
returntrue; //all formatting is good and we have a legitimate book def.
}//end of is_book definition
}//end namespace Book
#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.
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??
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.