C++ :: Accessing A Vector That Was Created In A Separate Header File?

Oct 26, 2014

i have this vector:

#ifndef new_thing_Inventory_h
#define new_thing_Inventory_h
#include <vector>
#include <string>
using namespace std;
class Inventory {

[code]....

so i know i need to use .push_back or .pop_back, but the program im using dosn't even recognize that inventory is a created vector.

View 6 Replies


ADVERTISEMENT

C++ :: Separating Routines Into A Separate Implementation And Header File

Oct 18, 2013

I am trying to separate out particular sets of routines into a separate implentation and header file which can be compiled independently to the main program such that the program source consists of a file called customers.h, customers.cpp, and exercise_1_5.cpp

Each of the files should contain the following:

customers.h should contain the definition of the customer structure and the declaration of print_customers.

customers.cpp should contain the implementation (or definition) for print_customers.

exercise_1_5.cpp should contain an include of customers.h and the main program.

This is my original code from a single .cpp file

#include<iostream>
#include<string>
using namespace std;

[Code].....

The error messages I am getting from the compiler on the customers.cpp file:

C:UsersBenDocumentsCS264lab3customers.cpp:5:22: error: variable or field 'print_customers' declared void
C:UsersBenDocumentsCS264lab3customers.cpp:5:22: error: 'customer' was not declared in this scope
C:UsersBenDocumentsCS264lab3customers.cpp:5:32: error: 'head' was not declared in this scope

View 6 Replies View Related

C++ :: Create User Defined Class And Have Access In Separate Header File From Main

Mar 15, 2013

I am a beginner with C++, taking a class right now. The lab this week is to create a user defined class and have it accesses in a separate .h header file from the main.

I think I'm finding my way through it, but I'm getting a complie error that makes no sense to me:

1> Resistor.cpp
1>c:users omdocumentsschoolcomp 220week 2lablab 2.2lab 2.2
esistor.h(11): error C2146: syntax error : missing ';' before identifier 'resistor'
1>c:users omdocumentsschoolcomp 220week 2lablab 2.2lab 2.2

[Code] .....

Here is the first portion of the .h file:

#include <iostream>
#include <iomanip>
#include <Windows.h>
#include <math.h>
#include <string>

class CResistor{
double res1, res2, res3, percentage, Nominal, Tolerance;

[Code] ....

View 16 Replies View Related

C++ :: Accessing Private Members From Header File

Dec 27, 2013

i have seen many c++ programs, where the private members from a header file are accessed in the source file. why is happening? As to my knowledge a private member cannot be accessed until it is friend function or member.

View 2 Replies View Related

C++ :: Accessing Data Declared Private Within Header File?

Feb 10, 2015

I have a header file that declares some fields as private, I then have a class that I need to compare two of the objects' information for equality but neither of them are the calling objects. I cannot alter the header file. How would I go about comparing private data fields? I will enter a brief bit of code for clarity.

Code: // Header File
// stuff.h

class stuff
{
private:
int* arr[20];
int size;
};
bool equal (const stuff& a, const stuff& b);

[code].....

View 11 Replies View Related

C++ :: Defining Vector In Header File?

Feb 17, 2012

Suppose I have the following sample header file test.h

Code:
#include "myCommon.h"
class Test {
public:
Test();
vector<vector<vector<double>>> vI3(dim1, vector<vector<double>> (dim2, vector<double> (dim2, 0.0f)));
private:
fillVector();
}

In above test.h dim1 and dim2 are defined in a different header file, i.e. myCommon.h

Code:
const long dim1 = 40;
enum dimVector {
RED,
GREEN,
dim2
};

However, it gives the errors when I compile: variable "dim1" is not a type name and for variable "dim2" it complains about a duplicate parameter name.

The declarations of dim1 and dim2 should stay in myCommon.h. They can also be defined in myCommon.cpp if needed, but can't go into test.h.

View 3 Replies View Related

Visual C++ :: Friend Classes In Separate Header Files?

Nov 25, 2012

I am struggling to enable friendship between two classes in separate header files for a banking program.

I am attempting to get the Person class to use variables from the Account class. Heres what I have so far.

ACCOUNT.h:

Code:
#include<iostream>
#include<fstream>
#include<cctype>
#include<iomanip>
#include <string>
#include <math.h>
#include <windows.h>
#include <vector>
#include "Person.h"
using namespace std;
class person;

[code].....

View 3 Replies View Related

C++ :: Forward Declaration Of Vector In Header File

Nov 7, 2014

I have a header file in which we include vector at the top. But we want to remove that include. On doing so I get compilation errors as the header file uses std::vector<> at several instances in header file so I have to forward declare the vector to solve this issue.how i can do it.

Header file :

#ifndef MKLMulticlassOPTIMIZATIONBASE_H_
#define MKLMulticlassOPTIMIZATIONBASE_H_
#include <shogun/lib/config.h>
#include <vector>
#include <shogun/base/SGObject.h>
namespace shogun {

[Code]...

View 13 Replies View Related

C/C++ :: How To Create A Vector In Header File That CPP Object Can Use

Apr 25, 2015

I'm working on a grocery store inventory project. One part is to have a shopping cart, where customers can put in up to 20 items. Because there can be up to 20 shopping carts at one time, I want to use a vector inside the cart object to represent all the individual food items.

Here's my code,

Header:

#ifndef CART_H
#define CART_H
#include <vector>
class Cart {
public:
Cart();
Cart(std::vector< int >, std::vector< int >)

[Code] ....

View 9 Replies View Related

C :: Accessing Static Functions In Header Via Macro

May 11, 2013

Just wandering, will something like this work? header

Code:

#define func(arg) func_(arg) ...
static void func_(arg); code Code: #include "header"
int main()
{
func(arg);
}

View 1 Replies View Related

C++ ::  passing 2D Array Created Using Vector Container

Feb 10, 2014

I am trying to pass a 2D array called f (coming from a text file with 9 columns of numbers and 297,680 rows) that was created using the vector container in my main() to the function myfunc. I'm just trying to figure out how to pass the address of f in main() to myfunc(), so that myfunc() has arguments consisting of a pointer g (that accepts the address of f as an input) and an int.

This is the error from the compiler:
test_2d.cc: In function ‘int main()’:
test_2d.cc:47:25: error: cannot convert ‘std::vector<std::vector<double> >*’ to ‘double (*)[297680][9]’ for argument ‘1’ to ‘int myfunc(double (*)[297680][9], int)’
return myfunc(&f,count);
^
Here is my code:

#include <iostream>
#include <fstream>
#include <iomanip> //allow setprecision to get all the decimal points
#include <vector>
#include <string>

[Code] ...

View 5 Replies View Related

C++ :: Accessing Value Of Map When It Is A Vector

Jan 30, 2013

I have the following map: myMap<string,vector<int>>. I now want to look through my map to see if a key exists and if it does, retrieve one of the int values from within that vector. What would be the best way to do this? Right now I have the following:

What would be the best way to look for a key and get one of the int values from its vector value? Right now I'm doing something like this:

[code]
map<string,vector<unsigned int>>::iterator it;
it = wordMap.find(someWord);
if(it == wordMap.end){
cout << "No matching entry";
} else{
// this is where I'd want to access the value (the vector) of the map
}

Is there a better way to do this?

View 2 Replies View Related

C++ :: Accessing Vector Objects Via A Pointer?

May 9, 2013

I have a pointer to a vector of objects, each object has an array and a couple of strings. how to access the data in the objects via the pointer.

Best tree::chooseSplit(vector <pgm> *ptr)
{
Best splits;
cout<<ptr[1].filePath; //not working!!!
}

filepath is a string in the pgm object. i also need to be able to access elements in an array that also exists in pgm.

View 2 Replies View Related

C++ :: Accessing Class Functions In A Vector?

Jul 24, 2013

How I can use class functions in a vector. I have 2 classes:

Gamemode class
bullets class

Inside the Gamemode class I declared: vector<bullets> Bullet and bullets * b.

If the user presses the shoot button a new bullets class will be added to the Bullet vector:

b = new bullets;
Bullet.push_back(b)

Bow I'd like to check all objects in the Bullet vector for the collision() function inside the bullets class.

I already set up a for loop which counts from 0 to the end of the vector:

for( int i=0;i<Bullet.size;i++)
{
}

My idea was to do something like:

if(Bullet[i].collision()) then erase Bullet[i]

But that doesn't work...

View 2 Replies View Related

C++ :: Accessing Nested Elements Of Vector

May 1, 2015

so lets assume i have a nested vector in a set or vice versa o in a more general form a container in a container example...

Code:
std::set<vector<int> > my_tuple;

How do i access individual elements of lets say the first vector in the set! Or the last element in the 3rd vector?

View 7 Replies View Related

C++ :: Accessing And Working With Vector From Multiple Threads

Oct 20, 2014

I have a vector that I would like to access and work with from multiple threads. I have created an example below to illustrate the functionality that I would like to accomplish.

The goals are to be (1) high speed, (2) thread safe, and (3) *if possible* continue to use vectors as my larger project uses vectors all over the place and as such I would like to maintain that.

However, if I need to switch from vectors to something else then I am open to that as well.

The following example below compiles but crashes rather quickly because it is not thread safe.

How I can fix the example below which I can then apply to my larger project?

#include <string>
#include <vector>
#include <ctime>
#include <thread>
#include <iostream>
#include <random>
#include <atomic>
#include <algorithm>
enum EmployeeType {

[Code] ....

View 1 Replies View Related

C++ :: Accessing Pointed-to Value In A Struct Vector Of Pointers?

Apr 30, 2013

I have a vector (structures) in a struct (instances). I make a declaration of this struct called instance. The vector is a 3-layer vector of pointers, like so:

vector < vector < vector<scene::IAnimatedMeshSceneNode*> > > structures; (The type is from Irrlicht 3D). I have 3 nested "for" loops which looks similar to the following:

for (int a = 0; a < instance.structures.size(); a++) { /*note:vector size previously set*/
for (int b = 0; b < instance.structures[a].size(); b++){
for (int c = 0; c < instance.structures[a][b].size(); c++) {

if (1) { //checking value of variable not included in snippet

(instance.structures)[a][b][c] = smgr->addAnimatedMeshSceneNode(fl);
(instance.structures)[a][b][c]->setPosition(renderPos);
}
}
}
}

The problem is in these two lines, I think:

(instance.structures)[a][b][c] = smgr->addAnimatedMeshSceneNode(fl);
(instance.structures)[a][b][c]->setPosition(renderPos);

These are currently referencing the pointers, it seems. The program compiles but crashes at this point. I need them to reference the values of the pointers. Problem is, I don't know where to put the dereference operator (*). Where should it go?

View 4 Replies View Related

C++ :: File Created But Empty

Jul 22, 2013

A file is created but its empty. And when I first create an entry and then display all the entries, it does nothing.

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;

class comp {

[Code] ....

View 2 Replies View Related

C++ :: Vector In Header That Holds One Of Other Classes?

Jan 10, 2015

I can not seem to add a vector to my head file. I have tried many things and can't figure it out.

Compile and Compile error -

g++ -Wextra -pedantic -std=c++11 Card.cpp Card.h Deck.h Deck.cpp unit_test1.cpp ;
Deck.h:15:27: error: invalid declarator before ‘deck’
void std::vector<Card *> deck;

[Code]....

View 2 Replies View Related

Visual C++ :: Using Fstream To Open A File Created Only During Runtime?

Nov 20, 2012

I'm using Visual C++ 6.0 and I'm trying to use fstream to open and read a file that is created only during runtime. This file is written by another function running on another thread, and my program will keep trying to "open" the file until it can be opened, i.e. after it's created, then read 3 numbers from it and execute the rest of its code.

The file test.txt has the content

Code:
1
3
4

My program that polls and opens the file is as follows:

Code:
ifstream fin;
std::string tfile, snum1, snum2, snum3;
long int num2, num3;
tfile.assign(argv[1]);
printf("Begin prog %s
", tfile.c_str());

[code]....

I executed the program by

Code: test_prog.exe "C: est.txt"

and waited about 3 seconds before putting the test.txt file into C:

My output was

Code:
Begin prog C: est.txt
Cannot open file C: est.txt
Cannot open file C: est.txt
Cannot open file C: est.txt
Cannot open file C: est.txt
Cannot open file C: est.txt
Cannot open file C: est.txt
fin is open
snum1 =
num2 = 0 num3 = 0
End of prog

The test.txt file disappears after I refresh the C: folder.

So the values for snum1, num2 and num3 are all wrong, as if the file was not read correctly.

If I put a while fin.good() loop after printf("fin is open "); for that entire block (until printing the values of num2 and num3), then I get

Code:
Begin prog C: est.txt
Cannot open file C: est.txt
Cannot open file C: est.txt
Cannot open file C: est.txt
Cannot open file C: est.txt
Cannot open file C: est.txt
Cannot open file C: est.txt
fin is open
End of prog

How can I correctly read a file that is only created during runtime?

View 4 Replies View Related

C++ :: Create Save File For A Game That Is Not A Separate File?

Oct 10, 2014

How do you create a save file for a game, that is not a separate file? Specifically I am using code::blocks and sfml 2.1 to make a game and it saves to a text file at the moment. My problem is that it is very easy to modify the text file, and it is annoying to have to copy and paste several files if you want to use a copy of the game. I have a feeling that it may be to do with resource files, but I'm not exactly sure how to get these to work or whether you can modify them dynamically.

View 6 Replies View Related

C/C++ :: How To Add Functions To Class In A Separate File

Jan 31, 2014

I could do the assignment if all i had to do was create a function within one file, call it in the main class, and that would be done with it. no, my teacher wants us to have our main program, a header and a separate class file and create functions in it in which we can then use in the main file.

This is the main file:

#include <iostream>
#include "call.h"
using namespace std;
int main() {
int length; //holds the length of the call in minutes
int hour; //holds the hour od the day(0-23 military time)

[Code] .....

View 7 Replies View Related

C++ :: Member Function Undefined In Separate File

Feb 25, 2015

I have three files.

//headerfile NumArrSpecs.h
#ifndef NUMARR_H
#define NUMARR_H

[Code].....

My problem is that the storeElems member function is causing an error saying it is undefined, however there are no errors any where else in the program being reported. I have made several programs involving classes now, all with this three file format and this is the first time that a member function in the main file is being reported as undefined, so I'm not sure what to do.

View 2 Replies View Related

C++ :: Read From A File And Store Information In Separate Variables?

Dec 8, 2014

So I have to read from a file, and store the information in separate variables. My problem is that for some of the information I need multiple words, and for others I don't, so I cant simply store 1 word into a variable and store the next in another in so on. To demonstrate what I mean let me give an example.

Dog Cat Blue Bird Snake White Horse

I am able to store "Dog","Cat","Blue","Bird"...etc in variables but I don't know how to make it so I can store "Dog" in one variable, "Cat" in a second variable. and "Blue Bird" in a third variable. "Snake" would be the 4th and "White Horse" would be the fifth. How can I read a file and manipulate the pointer to grab what I need?

View 2 Replies View Related

C++ :: Read Story From Input File / Separate Words And Output Lines On Which The Word Occurs

Feb 21, 2014

I have program that is supposed to read in a story from an input file and separate the words and output the lines on which the word occurs. It needs to read in another input file that has a list of words (1 per line) to ignore. i.e. skip them when running through the story. This is what I have so far, I've changed multiple things trying to get it running....

#include<iostream>
#include<fstream>
#include<map>
#include<set>
#include<vector>
#include<string>
#include"split.h"

[Code] .....

View 1 Replies View Related

C :: Segmentation Fault While Accessing DAT File In Unix

Oct 28, 2014

Below is my sample code of a C program. I am doing lot of processing, but my issue is I am getting a segmentation fault error in my main when I am putting the file names at the command line and using them. The program works great in Windows, but its giving error in Unix.

Below is the code of my Main function.

Code:

int main(int argc, char *argv[])
{
FILE *user, *requests;
char c;
user = fopen(argv[1], "r");
requests = fopen(argv[2], "r");
//For users
struct userStorage *USHead = NULL;
//struct users *userHead = NULL;

[Code]...

View 6 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved