0Dear StackExchange Community,
I have for two hours tried to find the source of the problem but failed completly. Research=google search also did not provide any viable solutions. At least I was able to discover that under VS 6.0 one cannot split the declaration and implementation of a template function between header and .cpp-file.
Perhaps my approach is inherently flawed or it is VS 6.0 that is being particulary obnoxious this time.
Here is the test code I wrote.
#include "stdafx.h"
#include <string>
#include <iostream>
class TestClass{
public:
template<class T> inline bool isNull(T& inObject){
return 0; // edited because of the answer by Joachim Pileborg :)
// initial code was: return (inObject != NULL) ? 0:1;
}
};
using namespace std;
int main(int argc, char* argv[])
{
cout<<TestClass::isNull<string>("test");
return 0;
}
Running this code causes the following error:
fatal error C1001: INTERNER COMPILER- FEHLER
(Compiler-File "msc1.cpp", Row 1794)
Does anybody have an idea what I am doing wrong here?
P.S. : this time i really endevoured to ask the question as precisly as possible also providing a concrete example. Please let me know if there is anything else I should have added.
P.SS: I know that visual studio 6.0 ist pretty old but I am forced to use it at work. Running the same code with the a new compiler (at home) did not cause any errors. This is why I assume that the problem is mainly caused by the whims of VS 6.0.
Thanks in advance for you help !!
JD
Unless you define a custom casting operator that returns a pointer, an object instance can never be equal to NULL.
Apart from facts noted in comments and answers, internal compiler error happens in situation, when there's a bug in the compiler, that prevents it from compiling valid code.
Microsoft usually fixes these bugs in IDE hotfixes or in newer versions of compilers. Try to modify the structure of code such that it does the same thing, but looks differently - it's the only way to avoid the Internal Error problem.
There are several troubles in your code:
I rewrote it this way:
comparing the adress of the reference you're passing (you have edited your question but you wrote inObject==NULL in the body of your function and it couldn't compile either)
using const string& so has to be able to call TestClass::isNull<string>("test");
you must define your function as static if you want to call it the way you do
I'm not sure but the character '<' following your word template looked badly encoded in my IDE, so I replaced it with a commonplace <, it compiled better
it's a way of coding, but prefer using typename than class when defining templates
prefer using true and false instead of 1 and 0 ( you edited your question but you still return 0...)
=>
#include <string>
#include <iostream>
class TestClass{
public:
template<typename T>
static bool isNull(const T& inObject)
{
return (&inObject == NULL) ? true : false;
}
};
using namespace std;
int main(int argc, char* argv[])
{
cout<< TestClass::isNull<string>("test");
return 0;
}
Now it compiles fine.
Related
I've read visual studio 2012 adding new header file, but my problem has not been solved! Anyway, I want to add foo.h to my project:
#pragma once
void MyLDA(vector<int>, Mat_<float>, Mat&, Mat&);
Now, foo.cpp:
#include "stdafx.h"
#include "foo.h"
using namespace std;
auto getIndices = [](const std::vector<int>& vec, const int value)
{
//some code
}
void MyLDA(vector<int> gnd, Mat_<float> _data, Mat &eigvector, Mat &eigvalue)
{
//some code
}
when i build my project, i get this error:
'vector': undeclared identifier
type 'int' unexpected
'my_project': identifier not found
You need to #include <vector> to use std::vector. Also, best practice is to avoid using namespace std; and instead write std::vector. Some also recommend that for OpenCV classes such as Mat. (For what it’s worth, my own coding style is to write std:: in front of STL classes, since there’s a lot of legacy code out there with a custom string or array class, but to write cout and memcpy() rather than things like std::cout, for stuff that was around long before the STL.)
There’s also a missing semicolon after the assignment of a lambda expression, which happens to look a lot like a function definition at a glance. (#BarmakShemirani caught it, not me.)
I'm trying to implement a thread-safe random number generator using an answer to a different post on this site. Xcode gives me a compile error in some system-provided source code. I've cut all the cruft out and this is the minimum code that will reproduce the error on the latest up-to-date Xcode.
#include <random>
#include <climits>
using namespace std;
mt19937 * _generator = NULL;
template <typename T> T ts_rand(void)
{
uniform_int_distribution<T> distribution(0, INT_MAX);
static bool fInited = false;
if (!fInited)
{
_generator = new mt19937();
fInited = true;
}
return distribution(_generator);
}
int main(int argc, const char * argv[])
{
int random_number = ts_rand<int>();
return random_number;
}
When I try to compile it, I get an error in the file 'algorithm', lines 2843 and 2865: "Semantic issue Type 'std::__1::mersenne_twister_engine * cannot be used prior to '::' because it has no members".
If I change _generator to be an actual instance instead of a pointer, it compiles fine. So that makes me think that there is something about this use of a templating that I don't understand, rather than an error in a system-provided file. FWIW, this same construct compiles and runs fine on VS 2013.
StackOverflow's suggested similar questions would seem to indicate that this might be related to VS' generous interpretations of incompletely-defined template classes, but I'm at a loss.
Any tips appreciated.
Your _generator is a pointer, but uniform_int_distribution::operator() expects a reference to a UniformRandomNumberGenerator.
You just need to do:
return distribution(*_generator);
#include <iostream>
#include <string>
using namespace std;
int main(){
string s = "wassup", newVal = "though";
cout << *(s.insert(s.begin(), newVal.begin(), newVal.end()));
}
This brings up the problem that the return type of string's insert member function is void (error: void value not ignored as it ought to be). This link indicates C++98 returns void but the "new" standard C++11 does indeed return an iterator.
A bit of context, I actually faced this problem earlier. I was/am using CodeBlocks (GCC Compiler Collection) on Windows 7 64-bit and this program gave the same issue (dereferencing void):
#include <iostream>
#include <list>
int main(){
list<int> x = {1,2,3,4};
*(x.insert(++x.begin(), 3, 2));
for(auto c : x)
cout << c;
}
I posted my issue on a different forum and a user pointed out Mingw32 was missing that particular C++11 change, indicating Mingw-w64 does not have this issue. So I went straight to installing Mingw-w64 on Code::Blocks using this guide and the problem was resolved. It's only now I've found out that the overloaded function which takes three iterator parameters STILL returns void.
I'm a little confused as to why Code::Blocks Mingw32 didn't supply a fully updated C++11 standard. Can anyone suggest a download that will definitely bring my compiler up to speed?
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
C++ void return type of main()
What is the proper declaration of main?
Simple question, really.
My friend and I are perusing the Powerpoint slides of a professor we are supposed to be hearing next semester. It will be a Java course. For some reason, he has this C++ code snippet
#include <iostream.h>
main ()
{ cout << "Hello, World\n"; }
I have told my friend, "No, this won't work with any modern C++ compiler."
My question is now, can this compile at all?
It could, sure.
Consider, for example, if <iostream.h> was a header with the following contents:
#include <iostream>
using std::cout;
#define main int main
That is not standard C++. The list of issues is quite long for a piece of code that short... probably because it comes from ages ago and an old non-conforming compiler.
The proper name of the include header is #include <iostream>, the .h was dropped during the ANSI standarization.
Types must be explicitly stated in C++. You cannot declare a function without return type and get a default int (that is C, not C++).
The standard signatures for main are: int main(), and int main( int argc, char** ) (implementations can provide extra arguments, but the return type must be int)
cout is defined inside the std namespace and cannot be used without qualification unless you add a using.
The equivalent code in proper C++ would be
#include <iostream>
int main() {
std::cout << "Hello world\n";
}
This will compile, though any decent compiler should raise a warning. Since you're using #include <iostream.h>, the compiler assumes that this is old code and compiles it in backwards-compatible mode. On my machine, gcc says:
In file included from /usr/include/c++/4.2.1/backward/iostream.h:31,
from oldcpp.cpp:1:
/usr/include/c++/4.2.1/backward/backward_warning.h:32:2: warning:
#warning This file includes at least one deprecated or antiquated header.
Please consider using one of the 32 headers found in section 17.4.1.2 of the C++
standard. Examples include substituting the <X> header for the <X.h> header for C++
includes, or <iostream> instead of the deprecated header <iostream.h>.
To disable this warning use -Wno-deprecated.
But it still compiles it fine. On running the code, I get exactly what is expected: i.e.:
Hello, World
Is this even worth questioning? Why write corner-case C/C++? What's wrong with keeping with the standards? main() is the entry point of the program, and the OS expects a return code from the program. Now there are arguments whether void main() is acceptable, but why even defend it? Just use int main() and be a happy dude that writes good code:
#include <iostream>
int main(int argc, char* argv[]) {
std::cout << "Hello World!\n";
return 0;
}
I start every program with this (without the HW! then) and never ever had any issues.
I have the following code snippet. I am compiling using the sun studio 12 compiler and have tried boost 1.33 and 1.39
#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
using namespace boost;
using namespace std;
int main(int argc, char* argv[])
{
string exbyte = "0x2430";
string exbytes = "0x2430,2430";
typedef vector< string > SplitVec;
SplitVec res1 ;
split(res1 , exbyte, is_any_of(",") );
return 0
}
I get the following compile error:
"/bb/source/boost/boost_1_39_0/boost/algorithm/string/iter_find.hpp", line 175: Error, nomatchoverin: Could not find a match for std::vector::vector(boost::transform_iterator, boost::algorithm::split_iterator, boost::use_default, boost::use_default>, boost::transform_iterator, boost::algorithm::split_iterator, boost::use_default, boost::use_default>) needed in boost::algorithm::iter_split, std::string, boost::algorithm::detail::token_finderF>>(std::vector&, std::string &, boost::algorithm::detail::token_finderF>)
If anybody has thoughts on this that would be awesome. Since I am cotemplateing strtok(only kidding)
Other than the missing semi-colon after return 0, which I assume is an unrelated typo, your code compiles fine for me, using gcc 4.3.2.
According to the documentation for boost::split, you're using the function correctly, so I don't think this is a coding error. Are you sure you have boost installed correctly?
Edit: It may be that Boost doesn't support your particular compiler, so parts of boost may not work for you. See here for a list of supported compilers, along with various issues which affect each compiler.
It sounds like your compiler's STL implementation only provides a vector ctor taking vector::iterator's and not any iterator class. You can verify this by taking a look at the vector header file.
You may able to work around this by using STLPort which apparently can be used with Sun Studio 12.