Pointer to struct defined in another file - c++

Is it possible to point to a struct which was defined in another file from within a class?
I tried it like in the code below but get this error:
cannot convert 'main()::list*' to 'entry::list*' in assignment
main.cpp:
#include "entry.h"
#include <vector>
int main()
{
struct list
{
std::vector<entry*> entryVector;
int temp;
};
list A;
entry B;
B.ptrToStruct = &A;
return 0;
}
entry.h:
#ifndef ENTRY_H
#define ENTRY_H
#include <string>
class entry
{
public:
struct list; //prototype does not work
std::string text;
struct list* ptrToStruct;
};
#endif // ENTRY_H
I also tried to write the prototype like this:
struct main::list;
That didn't work either because "'main' has not been declared".

This is about scope. When you declare list inside entry that is a new type entry::list.
If you want a global type list you should move the struct list; to the global scope outside the class.
You might want to do the same thing with the declaration inside main.

Related

Printing the first value from more than one vector in C++

I am trying to print the first value from each vector shown below in the main function.
#include <iomanip>
#include <map>
using namespace std;
typedef unsigned int vect;
int main() {
std::vector<vect> p;
vector<vect> a = { 4,2,3,1 };
vector<vect> b = { 4,2,3,1 };
vector<vect> c = { 4,2,3,1 };
vector<vect> d = { 4,2,3,1 };
int i;
for (i=0; i<a.size(); i++)
cout << a[i];
}
Function first_preference() from my function.cpp shown below
#include "function.h"
#include <string>
#include <iostream>
using namespace std;
person test::first_preference() const {
const person& first = p.front();
return first; //current first pref
}
The function is declared in this header class
#ifndef FUNCTION_H
#define FUCNTION_H
#include <vector>
typedef unsigned int person;
typedef unsigned int vect;
std::vector<vect> p;
class test {
public:
person first_preference() const;
};
#endif
I want the function first_preference() to be called from main() where the function should print the first value of each vector, how would I go about this?
I want the function first_preference() to be called from main() where the function should print the first value of each vector
Some issues:
You have a global std::vector<vect> p in your header file (which is not a good idea to begin with) which is shadowed by std::vector<vect> p in main. What you put in the p in main will not be accessible from instances of test. Those instances only knows about the global p.
You don't #include "function.h" in main.cpp so you can't create test objects in main.
If you #include "function.h" in main.cpp there's no need to typedef unsigned int vect; since you did that in function.h already. It's not an error, but confusing and unnecessary.
The vector<vect> instances a, b, c and d have no connection with test or any of the ps whatsoever so what you put in those vectors can't possibly be printed by instances of test unless you pass them on to test somehow.
You declare vectors of vect but first_preference() returns a person by value. vect and person happen to be aliases of the same fundamental type, but it seems like there is something wrong with this interface.
In main.cpp you don't instantiate a test, you iterate over a and first_preference() is never called so there's no hope for it to be used.
Why is “using namespace std;” considered bad practice?

How do I get values from a struct in a different cpp file?

I think there are many solutions outside for my problem but I dont get it, I'm kind of new to structs - so please help me..
OK my problem is I declare a struct in my header.h file and there is a function also inside that puts a string in one of the struct values and in the header file I can also output the string, but I want that struct and that !!value!! in a different cpp file where I can access to that value - so here is my code
header.h
#include <iostream>
#include <string.h>
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
struct FUNCTIONS
{
std::string f_name;
};
//extern FUNCTIONS globalStruct;
//put in struct variable
void put2struct()
{
struct FUNCTIONS struct1;
struct1.f_name = "FUNCTION";
std::cout << "Functionname: " << struct1.f_name << std::endl;
}
#endif //FUNCTIONS_H
and main.cpp
#include <iostream>
#include <string.h>
#include "header.h"
using namespace std;
int main(int argc, char const *argv[])
{
struct FUNCTIONS globalStruct;
put2struct();
//FUNCTIONS struct1;
std::cout << "Functionname2: " << globalStruct.f_name << std::endl;
return 0;
}
I hope somebody can help me I really dont get it how to do this :/
There is no way to directly access a local variable outside the block where it is defined. Because struct1 is an automatic variable, it is destroyed when put2struct returns, and no longer exists after that.
You can write a function that takes a FUNCTIONS by reference, and modify put2struct to call that function. That way you can access struct1 from a different cpp file:
void foo(FUNCTIONS&);
void put2struct()
{
FUNCTIONS struct1;
// do your thing
foo(struct1);
}
// another file
void foo(FUNCTIONS& object) {
// you have access to the object passed by reference
}

Data "member not declared in this scope"

I'm trying to create a vector which will store objects. I have added to the header file of the class as a private data member.
I am trying to initialize this vector as being empty (so that I can add objects to it later on in the program) but when I compile this program to test, this error is returned:
...error: '_bookingVector' was not declared in this scope|
I think the problem is with my initialization list on my default constructor(_bookingVector is obviously the vector):
Schedule::Schedule() : _bookingVector()
{ }
Is my syntax wrong? Or are vectors initialized differently?
Here is my code:
Schedule.h
#ifndef SCHEDULE_H
#define SCHEDULE_H
#include "Booking.h"
#include <vector>
using namespace std;
class Schedule
{
public:
Schedule();
void AddBooking(int bday, int btime, int btrainer, int bid);
void RemoveBooking(int bday, int btime);
void DisplaySchedule();
void DisplayAvailableTimeSlots();
//For Testing
void DisplayDebug();
private:
vector<Booking> _bookingVector;
};
#endif // SCHEDULE_H
Schedule.cpp
#include "Schedule.h"
#include "Booking.h"
#include <vector>
#include <iostream>
Schedule::Schedule() : _bookingVector()
{ }
void AddBooking(int bday, int btime, int btrainer, int bid){
Booking bookingObject(bday, btime, btrainer, bid);
_bookingVector.push_back(bookingObject);
}
void DisplayDebug(){
for(int i = 0; i < _bookingVector.size(); ++i){
cout << _bookingVecotr[i] << endl;
}
}
I'm very eager to learn what I'm doing wrong and fix it.
The issue is not with the constructor, which looks fine if unnecessary1. The issue is that you have defined AddBooking and DisplayDebug as non-member functions, but these should be members in order to access other members of the class.
Modify the definitions to be in the scope of the Schedule class thus:
void Schedule::AddBooking(int bday, int btime, int btrainer, int bid) { ...
^^^^^^^^^^
void Schedule::DisplayDebug(){ ...
^^^^^^^^^^
Also, don't say using namespace std in a header file (I'd go further and say don't say it anywhere but there isn't universal agreement on that.)
1 Your default constructor does not do anything that the compiler-generated one wouldn't do. You can safely remove it.

Why can't I make this object declaration in my class?

When I do this, my compiler complains. There are 3 errors that emerge, though no error messages visible:
#include <stdlib.h>
#include <vector>
#include <string>
#include "ParseException.h"
#include "CycleFoundException.h"
#include "UnknownTargetException.h"
using namespace std;
class Maker
{
private:
vector<Node> storage;
public:
Maker(string file) throw (ParseException, CycleFoundException, UnknownTargetException);
vector<string> makeTarget(string targetName);
};
struct Node
{
string target;
vector<string> dependencies;
string command;
int discoverytime;
int finishtime;
int visited;
Node* next;
};
The compiler does not like my vector<Node> storage declaration. When I do vector<int> storage instead, it compiles without complaint. Is it wrong to declare an object of one class in another class? I thought this was alright.
Looks like you need to put the definition of Node before the definition of Maker.
You use the type name Node in the definition of Maker (in the line vector<Node> storage), but because you haven't defined Node yet the compiler doesn't know what it is.

Class two has no member v error

I am new to C++ and I have a problem.I am trying to make to classes mutual friend,and accessing different members in one from the other one.I can't figure out what is that I do wrong.Here is what I have so far:
#ifndef HA_H_
#define HA_H_
#include"Ha2.h"
using namespace std;
class two;
class one{
public:
int tip;
int timp;
one(int t) :tip(t){}
friend class two;
};
#endif /* HA_H_ */
The second header:
#ifndef HA2_H_
#define HA2_H_
#include"Ha.h"
#include<vector>
class one;
class two{
public:
vector<one> v;
vector<int> x;
inline void create_x(vector<one> v){
// vector<one>::iterator it=v.begin();
int i;
for(i=0;i<v.size();i++){
x.push_back(v.at(i).tip);
}
}
friend class one;
};
#endif /* HA2_H_ */
And the main:
#include<vector>
#include<iostream>
#include"Ha.h"
#include"Ha2.h"
int main()
{
one o(3);
two x;
x.v.push_back(o);
x.create_x(x.v);
cout<< x.x.back();
}
And I get several errors like:
class two has no member named 'v'
Any advice?Thank you.
main.cpp includes "Ha.h", which, before using namespace std;, includes "Ha2.h". Then, class two is defined, which declares v as a vector<one> without qualifying vector in the std namespace.
There is no need to include "Ha2.h" in "Ha.h", remove that, then qualify vector.
Forward declarations are used when the class definition has pointers or references. And neither of the class definitions has them and so forward declarations are unnecessary.
Also the headers has both forward declarations and it's corresponding header inclusion which voids the whole purpose of forward declarations.
Include the headers and also the using directive in the source files.
As of now, the program has no methods to test the purpose of friend and so this should at least make the program compile.
Ha.h header :
#ifndef HA_H_
#define HA_H_
class two ; // No pointers or references of two in one.
// So, remove it and place when you actually have
// a method taking reference of two.
class one{
public:
int tip;
int timp;
one(int t) :tip(t){}
friend class two;
};
#endif /* HA_H_ */
Ha.cpp
#include "Ha2.h" // Place this only when have a method accessing
// members of two. Else this is unnecessary.
#include "Ha.h"
using namespace std ;
Ha2.h
#ifndef HA2_H_
#define HA2_H_
class one ; // No pointers or references of one in two.
// So, remove it and place when you actually have
// a method taking reference of one.
class two{
public:
vector<one> v;
vector<int> x;
inline void create_x(vector<one> v)
{
// vector<one>::iterator it=v.begin();
int i;
for(i=0;i<v.size();i++)
{
x.push_back(v.at(i).tip);
}
}
friend class one;
};
#endif /* HA2_H_ */
Ha2.cpp
#include <vector>
#include "Ha.h" // Place this only when have a method accessing
// members of two. Else this is unnecessary.
#include "Ha2.h"
using namespace std ;
// .....
main.cpp
#include <vector> // Definitely need this because in the current unit, Ha2.h is
// included which has a data type of vector<int>
#include <iostream>
#include"Ha.h"
#include"Ha2.h"
using namespace std ; // vector and the other standard definitions are specified
// in std namespace
int main()
{
one o(3);
two x;
x.v.push_back(o);
x.create_x(x.v);
cout<< x.x.back();
}
With the above modifications, you should get rid of errors. If any new errors pop up, post the exact error messages in your question.
Another alternative to remove the circular dependency is to make a separate header file, like decl.h or whatever you want it to be, and inside of that simply put the following lines:
#ifndef DECL_H_
#define DECL_H_
class one;
class two;
#endif DECL_H_
Then in Ha.h and Ha2.h remove the #include "Ha.h" and #include "Ha2.h" lines and replace them with #include "decl.h". Also remove the class one; and class two; declarations in Ha.h and Ha2.h, but keep the definitions of each respective class.
Now you won't have Ha.h depending on Ha2.h and vice versa.
Hope this helps,
Jason