I don't see what's wrong with the following code. WordStore.cxx is defined similarly to have an empty function body. The compiler is complaining that "expected ')'" in the semstore.h function definition. I'm using XCode.
Incidentally, I'm upgrading some ancient (10+ year-old) code to compile on a modern C++ compiler.
/* WordStore.h */
#ifndef WORD_STORE_H
#define WORD_STORE_H
class WordStore
{
public:
WordStore();
};
#endif
// semclass.h
#ifndef SEMCLASS_H
#define SEMCLASS_H
#include <iostream>
using namespace std;
void ReadSemRules(std::istream& stream, WordStore& ws);
#endif
// semclass.cxx
#include <iostream>
#include <string.h>
#include "WordStore.h"
#include "SemClass.h"
using namespace std;
void ReadSemRules(istream& stream, WordStore& ws)
{
}
You have stray unprintable character in your program between the m and the &:
https://godbolt.org/g/gAAoGn
void ReadSemRules(std::istream& stream, WordStore& ws);
^^
Related
My code is similar to this one, but the problem is exactly the same: I'm getting an "undefined reference to `Test1::v" in Test1.cpp and in Test2.cpp when compilling the program in VSCode. What am I doing wrong? I'm a bit new on c++ so I just downloaded an extension that made me a project in c++ automatically. When I run the program using Ctrl + Shift + B it gives me this error, but when I do it with the Code Runner extension it doesn't detect the .cpp files.
// Test1.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST1_H
#define TEST1_H
class Test1{
public:
Test1();
static vector<Test1> v;
int a;
};
#endif
//Test1.cpp
#include "Test1.h"
Test1::Test1(){
a = 2;
v.push_back(*this);
}
//Test2.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST2_H
#define TEST2_H
class Test2{
public:
Test2();
double var;
};
#endif
//Test2.cpp
#include "Test2.h"
#include "Test1.h"
Test2::Test2(){
var = 5;
Test1::v[0].a += var;
}
//main.cpp
#include <iostream>
#include "Test1.h"
#include "Test2.h"
using namespace std;
int main(int argc, char *argv[])
{
cout << "Hello world!" << endl;
}
You have declared the static vector in the header file, but you need to define it in a cpp file. Add:
vector<Test1> Test1::v;
to your test1.cpp file. You can learn more about definition vs declaration here.
Also make sure you read this: Why is "using namespace std;" considered bad practice?
You could prepend the class name to call the variable directly since it's static. So, you could do something like:
Test1::Test1(){
// v.push__back(*this); // previous
Test1::v.push_back(*this); // now
}
in Test1.cpp. You'll then get a reference tooltip on your VS Code:
static std::vector<Test1> Test1::v
Which proves it's done.
I have been having a lot of trouble with my headers and making sure everything is declared correctly. First off my files:
//Main.cpp
#include "Item.h"
#include "Warehouse.h"
using namespace std;
int main() {
...
}
//Item.h
#ifndef ITEM_H
#define ITEM_H
#include <string>
using namespace std;
class Item {
...
};
#endif /* ITEM_H */
//Item.cpp
#include "Item.h"
//Warehouse.h
#define WAREHOUSE_H
#ifndef ITEM_H
#define ITEM_H
using namespace std;
class Item;
class Warehouse {
...
private:
Item* array; //problem starts with this
};
#endif /* WAREHOUSE_H */
//Warehouse.cpp
#include "Warehouse.h"
#include "Item.h"
Warehouse::Warehouse() {
array = new Item[arraySize]; //and this is where I get the error
}
I am pretty sure the problem has to do with my header in Warehouse.h but every combination I try does not work. Sorry if not enough of the code is posted but I figure the problem is with the includes and declarations.
Thanks ahead of time.
edit: to clarify this is not in one file. I just wrote it like this to simplify things. Each one of the above is a separate file.
Your include guards in the header file Warehouse.h are not correct.
Instead of
//Warehouse.h
#define WAREHOUSE_H
#ifndef ITEM_H
#define ITEM_H
using namespace std;
// ...
#endif /* WAREHOUSE_H */
you want
//Warehouse.h
#ifndef WAREHOUSE_H
#define WAREHOUSE_H
using namespace std;
// ...
#endif /* WAREHOUSE_H */
With the current version the class definition in item.h is never included in Warehouse.cpp because the mixed-up include guards in Warehouse.h prevent item.h to be read due to the order of
//Warehouse.cpp
#include "Warehouse.h"
#include "Item.h" //Warehouse.cpp
#include "Warehouse.h"
#include "Item.h"
Then the compiler does not know the definition of Item at that point, hence the error.
Another thing: Do not form the habit of using namespace std in header files. This will lead to issues at some point.
Problem is not in this declaration
private:
Item* array; //problem starts with this
You may define a pointer to an incomplete type.
I think the problem is in a statement where you try to allocate an object for this pointer using operator new or to dereference the pointer.
Also I do not see any reason why you do not want to include header Item.h in header Warehouse.h instead of using elaborated name
class Item;
I am wondering why I am getting error "string could not be resolved to type" when I have the proper inclusions?
#ifndef EVENTFILEREADER_H_
#define EVENTFILEREADER_H_
#include <string>
#include <stdlib.h>
#include <iostream>
class EventFileReader {
public:
EventFileReader(string fileName);
virtual ~EventFileReader();
};
#endif /* EVENTFILEREADER_H_ */
Your compiler is complaining about not being able to find string as a defined type.
You should add its namespace std:
EventFileReader(std::string fileName);
^^^^^
You need to specify namespace, e.g.
std::string
or put the using declaration after includes:
using std::string;
I've installed the mimetic library according to the INSTALL instructions.
the following main file compiles without a problem with a gcc-c++ 4.1.2,
but when I upgrade to gcc-c++ 4.4.7 I get an error.
mimetic.cpp:
#include <iostream>
#include <mimetic.h>
using namespace std;
using namespace mimetic;
int main()
{
MimeEntity me;
return 0;
}
error
In file included from /usr/local/include/mimetic/rfc822/header.h:18,
from /usr/local/include/mimetic/header.h:11,
from /usr/local/include/mimetic/mimetic.h:18,
from mimetic.cpp:2:
/usr/local/include/mimetic/rfc822/messageid.h:29: error: expected ‘)’ before ‘thread_id’
the header file:
rfc822/messageid.h
#ifndef _MIMETIC_MESSAGEID_H_
#define _MIMETIC_MESSAGEID_H_
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <string>
#include <mimetic/libconfig.h>
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#include <mimetic/utils.h>
#include <mimetic/os/utils.h>
#include <mimetic/rfc822/fieldvalue.h>
namespace mimetic
{
/// Message-ID field value
/// On Win32 Winsock library must be initialized before using this class.
struct MessageId: public FieldValue
{
MessageId(uint32_t thread_id = 0 ); // <------ line 29
MessageId(const std::string&);
std::string str() const;
void set(const std::string&);
protected:
FieldValue* clone() const;
private:
static unsigned int ms_sequence_number;
std::string m_msgid;
};
}
#endif
is there some compatibility switch for the gcc ?
So, the problem turns out to be poor configuration of the system, which leaves HAVE_STDINT_H not configured, and thus the uint32_t doesn't get defined, and the error occurs.
I'm at a loss - i'm just getting into C++ and for some reason this is not working out for me. So i'm using Netbeans, and i've got the following main file:
#include <cstdlib>
#include "functions.h"
using namespace std;
int main(int argc, char** argv) {
f("help");
return 0;
}
Functions.h file:
#include <string>
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
void f( string a );
#endif
and Functions.cpp file:
#include "functions.h"
void f( string a ) {
return;
}
So, long story short, it doesn't compile. It says it can't understand the string variable? I don't get it, i tried moving the include for string all over the place but nowhere seems to help. What do i do?
If you are attempting to use std::string, you have to #include <string> in your functions header, and call it std::string,since it is in the std namespace.
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <string>
void f( std::string a );
#endif
See this related post and also why is 'using namespace std' considered bad practice in C++?
You need to include string header file in Functions.h, also tell compiler that string is from std namespace.
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <string>
void f( std::string a );
#endif
Functions.cpp file:
#include "functions.h"
void f( std::string a ) {
return;
}
Better practice is to pass string by const reference
void f(const std::string& a ) {
return;
}
See Why is 'using namespace std;' considered a bad practice in C++?
Include the standard header: <string>
#include <string>