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.)
Related
So I was trying to access a method that is defined in another class and has the prototype in the header. I'm pretty positive I defined it but it keeps popping up undefined reference to SafeCracker.
Main.cpp
#include <iostream>
#include "mystuff.h"
using namespace std;
void BigDog(int KibblesCount);
int main()
{
cout << SafeCracker(1);
return 0;
}
mystuff.cpp
#include <iostream>
using namespace std;
string SafeCracker(int SafeID)
{
return "123456";
}
mystuff.h
using namespace std;
#ifndef MYSTUFF_H_INCLUDED
#define MYSTUFF_H_INCLUDED
string SafeCracker(int SafeID);
#endif // MYSTUFF_H_INCLUDED
Here it tells you that you have an undefined reference, so you don't really have a problem with the prototype.
Had you forgotten to include the header file that contains the prototype you would have gotten something like
main.cpp: In function ‘int main()’:
main.cpp:8:13: error: ‘SafeCracker’ was not declared in this scope
cout << SafeCracker(1);
Your undefined reference is a linker error. The most likely cause would be that you did not use mystuff.cpp when compiling
If you're compiling from the command line, you should give both files as parameters.
If you're using an IDE that calls the compiler, make sure that the file is part of the project.
For example in Code::Blocks right-click on the file name and go "add to project" (If I remember correctly)
It is also possible that you made a typo in the function declaration in mystuff.cpp (that doesn't seem to be the case here though)
Now there is one important thing about your code you should take note of:
It is very bad practice to put a using namespace in a header file.
using namespace std; in a .cpp source file is mostly up to you, and that using statement will only apply to that particular file.
But if you put it in a header file that is meant to be included through #include , the using there will be forced upon any code that includes it.
Here is an example:
main.cpp
#include <iostream>
// including mystuff.h to use that awesome SafeCracker()
#include "mystuff.h"
// I need to use an std::map (basically an associative array)
#include <map>
// the map of my game
class map
{
int tiles[10][10];
};
int main()
{
// The std map I need to use
std::map<int, int> mymappedcontainer;
// The map of my game I need to use
map mytiles;
// The reason why I need to include mystuff.h
cout << SafeCracker(1);
return 0;
}
Normally, my class map should not be a problem since the map I included from the standard library is inside the namespace std, so to use it you would need to go std::map.
The problem here is that, since mystuff.h has using namespace std; in it, the symbol map is already used, and that creates a conflict.
You do not now who will use your header files, or if you will use them again a long time from now, and maybe then you will want to use name that is already used in the std namespace.
I advise you to use std:: before things taken from the standard libraries instead (std::string instead of just string for example)
PS: In C++, "class" refers to a class data structure, and the functions you made here are not part of any class. You should say "defined in another file" or "defined in another translation unit" instead
So I'm learning to use a class .h and .cpp files in my program that reads a file containing information about a bank account. Initially the code worked fine, however after creating the .h and .cpp class files, things don't work so smoothly anymore, as I'm getting strange errors that don't make sense to me.
This is my MAIN cpp file:
#include "Bankaccount.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{ string fileName;
cout << "Enter the name of the data file: ";
cin>>fileName;
cout<<endl;
bankAccount object(fileName);
return 0;
}
This is my Bankaccount.h file
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include <iostream>
#include <fstream>
#include <string>
class bankAccount
{
public:
bankAccount(string n);
bankAccount();
private:
ifstream sourceFile;
}
And lastly this is the Bankaccount.cpp file
#include "Bankaccount.h"
#include <iostream>
#include <fstream>
#include <string>
bankAccount::bankAccount(string n)
{
sourceFile.open(n.c_str());
}
Which is now generating these errors:
include\Bankaccount.h|13|error: expected ')' before 'n'|
include\Bankaccount.h|18|error: 'ifstream' does not name a type|
include\Bankaccount.h|14|note: bankAccount::bankAccount()|
include\Bankaccount.h|14|note: candidate expects 0 arguments, 1 provided|
include\Bankaccount.h|4|note: bankAccount::bankAccount(const bankAccount&)|
include\Bankaccount.h|4|note: no known conversion for argument 1 from 'std::string {aka std::basic_string}' to 'const bankAccount&'|
I think it might be an issue with the headers? I went a little bit crazy and put all of my relevant headers on each file trying to get it to work.
using namespace std;
This is considered a bad programming practice, and you will do yourself a favor if you forget that this is actually a part of C++ language. Although there are proper situations where one would employ using namespace, this should be avoided until one has a much better technical understanding of C++, its structure, and its grammar; in order to recognize and understand when this can be used correctly (if at all).
In your main() you have:
string fileName;
There is no such class in the C++ library whose name is string. The class's correct name is std::string; however by shoving using namespace std; a few lines above, you end up blissfully unaware of this basic, fundamental fact.
Now, after you understand this, let's go back and look at your header file:
ifstream sourceFile;
Well, there's no such class in the C++ library called ifstream, either. The class's proper name is std::ifstream. All classes and templates from the C++ library exist in the std namespace.
However, because when you #included the header file your using namespace std; alias is not yet defined, your compiler doesn't recognize the class name, and you get this compilation error as a reward.
The solution is not to cram a using namespace std; in your header file. That will simply lead to more chaos and confusion. The proper fix is:
Remove using namespace std; from your code, completely.
Use full names of all classes from the C++ library, everywhere. Replace all references to string, ifstream, and everything else, with their actual class names: std::string, std::ifstream, and so on. Get into the habit of explicitly using the std namespace prefix every time. It might seem like a bother at first, but you'll quickly pick up the habit before long, and you won't think of it twice.
And you'll never be confused by these kinds of compilation errors ever agin.
class Space2D {
public:
vector<Agent> v;
bool star;
Space2D() {
bool star = false;
}
};
In visual studio this give me a error: missing type specifier - int assumed. I also get errors like syntax error: missing ';' before '<'.
std::vector<Agent> v; Solved this
Three possible problems with this code.
Possibly vector header is not included
Fix: #include <vector>
Possibly Agent class is not defined
Fix: include header, where Agent class is defined
Possibly you forgot to write using namespace std; as wrongly recommended by beginner books
Fix: instead of 'vector' on line 7 use std::vector, or do it wrong and write using namespace std;
Assuming from two error messages you posted and by guessing to which line they correspond to you forget to #include <vector> or you do not have imported std::vector to your namespace (using std::vector; or using namespace std).
I personally would not recommend using either of those usings for reasons and instead wrote std::vector.
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.
I was doing a project for computer course on programming concepts. This project was to be completed in C++ using Object Oriented designs we learned throughout the course. Anyhow, I have two files symboltable.h and symboltable.cpp. I want to use a map as the data structure so I define it in the private section of the header file. I #include <map> in the cpp file before I #include "symboltable.h".
I get several errors from the compiler (MS VS 2008 Pro) when I go to debug/run the program the first of which is:
Error 1 error C2146: syntax error : missing ';' before identifier 'table' c:\users\jsmith\documents\visual studio 2008\projects\project2\project2\symboltable.h 22 Project2
To fix this I had to #include <map> in the header file, which to me seems strange.
Here are the relevant code files:
// symboltable.h
#include <map>
class SymbolTable {
public:
SymbolTable() {}
void insert(string variable, double value);
double lookUp(string variable);
void init(); // Added as part of the spec given in the conference area.
private:
map<string, double> table; // Our container for variables and their values.
};
and
// symboltable.cpp
#include <map>
#include <string>
#include <iostream>
using namespace std;
#include "symboltable.h"
void SymbolTable::insert(string variable, double value) {
table[variable] = value; // Creates a new map entry, if variable name already exist it overwrites last value.
}
double SymbolTable::lookUp(string variable) {
if(table.find(variable) == table.end()) // Search for the variable, find() returns a position, if thats the end then we didnt find it.
throw exception("Error: Uninitialized variable");
else
return table[variable];
}
void SymbolTable::init() {
table.clear(); // Clears the map, removes all elements.
}
My guess is that you have another file that includes the header file #include "symboltable.h". And that other source file doesn't #include <map> nor #include <string> nor has using namespace std before it includes "symboltable.h".
Check which file is being compiled when you get the error. Is it maybe a different source file than the .cpp that you mentioned? Possibly something like main.cpp?
Another way to solve your problem is to put the includes you need in your header file and use std::map instead of simply map. Also you use string which is also inside the namespace std. So that needs to be std::string. And put the missing #include <string>.
Yes, you indeed have to #include <map> in the header file.
You use map in the declaration of the class, so the compiler needs to know what this map refers to. Since the definition of it is in <map> you need to include that header before using the map template class.
You could also instead #include <map> in every source file before the #include "symboltable.h" line, but usually you would just include these kind of prerequisites in the header.