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


ADVERTISEMENT

C++ :: Overloaded Operator Defined In Header File - Gives Error In CPP File Of Class

Apr 12, 2014

I am working on an assignment in which i have to perform th following task

myClass itsObject1,itsObject2;
itsObject2=5000+itsObject1;

I have defined overloaded operator as follows in the header file but in the cpp file of the class it gives error.

friend vli &vli::operator + (int &a,vli &obj);

How to define it in cpp file of my class?

View 1 Replies View Related

C :: Return Value From User Defined Function To Main

Nov 14, 2014

I want to return value from user defined function to main.

Code:

#include <stdio.h>
int mult ( int x, int y );
int add(int x, int y);
int loop (int y);
int main() {

[Code] ......

View 1 Replies View Related

C++ :: Cannot Access Private Member Declared In Class (header File)

Apr 1, 2014

I am currently doing the assignment about linked list. Here are some details information about what I am doing.. This program is C++ and should run on Visual Studio 2010. And it contains three file, two datastructure header and one main cpp file.

This program is trying to arrange and show some sports records. The main program which contain the functions such as reading the result text file(each result text file contain several records of athletes), removing a file, arranging the totalresult and printing it out. And the main program is already given and I cannot overwrite it.

But when I finished and try to build the solution and run it, I am not able to run the program and it give me somethings like these...

warning C4172: returning address of local variable or temporary
error C2248: 'Datastructure1::Datastructure1' : cannot access private member declared in class 'Datastructure1'
see declaration of 'Datastructure1::Datastructure1'
see declaration of 'Datastructure1'
This diagnostic occurred in the compiler generated function 'Result::Result(const Result &)'

And I have tried to comment each function part of the header file and see if can run or not. But I still fail to do so. Here are my codes...

#ifndef DATASTRUCTURE1_H
#define DATASTRUCTURE1_H
class Datastructure1 {
Public:
Datastructure1( );

[Code] ....

There are two header files and look quite long. They are all some linked list functions . I have read and learn linked list data structure before I complete this programs. However, when I complete the functions required, the function cannot be compile....

View 11 Replies View Related

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 View Related

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/C++ :: Create User Defined Dynamic Array For Player Scores - Missing Pointer Types Error

Jan 18, 2015

I'm completely new to pointers and have a homework assignment due where I'm supposed to create a user defined dynamic array for player scores. The only errors I'm experiencing is a C2109: subscript requires pointer type and only on the lines that use the int *score; variable (57, 62, 64, 69, 71, and 82). I've already tried adding = nullptr to the variable and that didn't work.

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
void players(string[], int[], int);
void average(int[], int);

[Code] ....

View 3 Replies View Related

C++ :: Binary Search Tree Used With User Defined Class

Nov 29, 2014

I am working on a program that uses a class I created called Student. I want to be able to add different students to a Binary Search Tree, and use the student's gpa (grade point average) to compare students with each other and place them in the correct location in the Tree.

Student class:

#ifndef STUDENT_H
#define STUDENT_H
#include <string>
#include <iostream>
#include <iomanip>
class Student{
friend ostream& operator<<(ostream& out, const Student& stu);

[Code] ......

View 3 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++ :: How To Create Header File

Oct 22, 2013

Any good/solid example on how to create a header file?

View 2 Replies View Related

C++ :: Create Header File In Which Can Change Value Of Variables

Apr 12, 2013

I just can't seem to find how to create a header file and where I can change the value of the variables. It sounds simple.

View 12 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

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/C++ :: Including Main Header File Which Includes The Including Head

Mar 28, 2015

I have a main.h file where I include all the needed things to make my program compile properly, string, vector etc.

And I also have another header file which comntains a class that is used later in the code (globally), I decided to keep it in another file to make it more clear and easier.

I need to include that file in main.h, but I also include main.h from that class header file because it contains some other includes that are required to compile.

Is this a good thing? Or should I keep main.h out of that class header file and include just things required for the class?

I may have complicated it too much, so I'll show an example, what I do now:

// ---- main.h ----
#include <string>
#include <vector>
#include <ctime>
// other includes, these are just examples
#include "MyClass.h" // the separated class header file

[Code] ......

So, from what you can see MyClass.h requires just including the vector, but to avoid repeating myself I include main.h which does that already, but also includes MyClass.h

So, I have two questions:
1. Is it ok to include in that way (including a file that includes the including file)
2. Is it good to include a main header file with all the includes even if I just need one of them, or should I skip including main.h and include just the things my class requires (vector is just an example)

View 2 Replies View Related

C++ :: Create File With Given Size And Then Randomly Access File To Populate It

Feb 17, 2015

I am interested in creating a file of a given size and then randomly accessing the file to populate it. Is there a way to quickly create, for instance, a 4 GByte file in C++, initially populated with garbage?

View 4 Replies View Related

C :: Save Data To Arrays Defined In Main Using Their Addresses

Dec 30, 2013

so i have two classes ( main and another one ask ) in main i have defined 3 arrays (char drivers[250] ,offences[250] , owners[250]) and also included their pointers ( char *drivers_ptr,*offences_ptr,8owners_ptr)my problem is that i need to set values (actually 1 string of 250 chars to each of these arrays BUT from the class ask without making these arrays global -as this is prohibited by my university exercise !- )

so my question is how will i manage to save data to the arrays that i have defined in main using their addresses(through the pointers of each one that i have passed to the ask class) from the ask class ?

View 4 Replies View Related

C++ :: Generate Report Based On Input Received From Text File - User Defined Namespace

Nov 11, 2014

Write a program to generate a report based on input received from a text file. Suppose the input text file student_status.txt contains the student’s name (lastName, firstName middleName), id, number of credits earned as follows :

Doe, John K.
3460 25
Andrews, Susan S.
3987 72
Monroe, Marylin
2298 87
Gaston, Arthur C.
2894 110

Generate the output in the following format :

John K. Doe 3460 25 Freshman
Susan S. Andrews 3987 40 Sophomore
Marylin Monroe 2298 87 Junior
Arthur C. Gaston 2894 110 Senior

The program must be written to use the enum class_level :

enum class_level {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR } ;

and define two namespace globalTypes (tys and fys) for the function :

class_level deriveClassLevel(int num_of_credits) ;

The function deriveClassLevel should derive the class_level of the student based on the number of credits earned.

The first namespace globalType tys should derive the class level based on a two year school policy.
and the second namespace globalType fys should derive the class level based on a four year school policy.

Four Year School Policy:
Freshman 0-29 creditsSophomore 30-59 credits
Junior 60-89 creditsSenior 90 or more credits

Two Year School Policy:
Freshman 0-29 creditsSophomore 30 or more credits

NOTE : use ignore() function with ifstream objects whenever you want to ignore the newline character.

For example :
ifstream transferSchoolFile ;
transferSchoolFile.open("student_status.txt", ios::in);

while( !transferSchoolFile.eof()) {
getline(transferSchoolFile,name) ;
transferSchoolFile >> id >> credits;
transferSchoolFile.ignore(); //Used here to ignore the newline character.
….
}

I did this in parts so I got it working with a four year criteria without the user defined name spaces.

include <iostream>
#include <fstream>
#include <string>

using namespace std;
enum class_level { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR };

[Code] ....

So that worked fine then I tried with name spaces -

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
enum class_level { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR };
class_level classLevel;

[Code] ....

I know I have some stuff to mess around with but I am currently stuck with two errors, first -

Error1error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartupC:UsersstephenDocumentsVisual Studio 2013ProjectsinputConsoleApplication1MSVCRTD.lib(crtexe.obj)ConsoleApplication1

then -

Error2error LNK1120: 1 unresolved externalsC:UsersstephenDocumentsVisual Studio 2013ProjectsinputDebugConsoleApplication1.exeConsoleApplication1

View 1 Replies View Related

C/C++ :: Generate Report Based On Input Received From Text File - User Defined Namespace

Nov 11, 2014

Program Description: Write a program to generate a report based on input received from a text file. Suppose the input text file student_status.txt contains the student's name (lastName, firstName middleName), id, number of credits earned as follows :

Doe, John K.
3460 25
Andrews, Susan S.
3987 72
Monroe, Marylin
2298 87
Gaston, Arthur C.
2894 110

Generate the output in the following format :

John K. Doe 3460 25 Freshman
Susan S. Andrews 3987 40 Sophomore
Marylin Monroe 2298 87 Junior
Arthur C. Gaston 2894 110 Senior

The program must be written to use the enum class_level :

enum class_level {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR } ;

and define two namespace globalTypes (tys and fys) for the function :

class_level deriveClassLevel(int num_of_credits) ;

The function deriveClassLevel should derive the class_level of the student based on the number of credits earned.

The first namespace globalType tys should derive the class level based on a two year school policy.
and the second namespace globalType fys should derive the class level based on a four year school policy.

Four Year School Policy:
Freshman 0-29 creditsSophomore 30-59 credits
Junior 60-89 creditsSenior 90 or more credits

Two Year School Policy:
Freshman 0-29 creditsSophomore 30 or more credits

NOTE : use ignore() function with ifstream objects whenever you want to ignore the newline character. For example :

ifstream transferSchoolFile ;
transferSchoolFile.open("student_status.txt", ios::in);
while( !transferSchoolFile.eof()) {
getline(transferSchoolFile,name) ;
transferSchoolFile >> id >> credits;

[Code] ....

I know I have some stuff to mess around with but I am currently stuck with two errors, first -

Error1error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartupC:UsersstephenDocumentsVisual Studio 2013ProjectsinputConsoleApplication1MSVCRTD.lib(crtexe.obj)ConsoleApplication1

then -

Error2error LNK1120: 1 unresolved externalsC:UsersstephenDocumentsVisual Studio 2013ProjectsinputDebugConsoleApplication1.exeConsoleApplication1

View 2 Replies View Related

C/C++ :: Allow User To Create A New Class Instance?

Dec 4, 2014

I am creating a program that allows a user to create multiple 'sequences' and multiple 'filters' and then apply a filter to a sequence.

Each sequence and filter is an array of values.

How do I go about allowing the user to create a 'new' sequence and then store the location so I can access it again later? Once they have created a new sequence they can go on to create another sequence and ten maybe a filter and then another sequence etc etc .. and then they can select sequence 1 and edit the values if they so wish.

They would be asked how many sample values for the sequence, and then I would create a sequence with that many values and an id (1,2,3,4...). They could then enter this id to view/edit the sequence.

The entering/editing values part I am fine with. I just don't know how to allow them to create multiple new instances of a class without using an array so something like..

sequenceClassName somearray[10];
int i;
*create a new array*
somearray[i].create_class(how_many_samples)
i++ //so next sequence they create is 2,3,4.. etc

- this then calls the member function that creates an array using 'sample_values = new float[how_many_samples]' and the user can input their data and edit it whenever by entering the id which will correspond to the somearray[i].

However that approach only allows them to enter a maximum of 11 sequences. It all depends on how big I make that initial array and it just seems like the wrong way to do it.

( how to interact with them, just how to create multiple classes and recall them later to access the data!)

View 2 Replies View Related

C Sharp :: Create Instance Of Form Into Non Form Class To Access Button / Label

Nov 23, 2014

I have a non form class. I want to update label/ check status of check box etc.. in non form class ( here resides functions that contains logic). How can i do that ?

View 4 Replies View Related

C++ :: How To Use Header File For A Class

Sep 21, 2014

I wrote a simple date class and could not get it to work until I put all the code in main(). Then it worked like a charm. I have not been able to create a separate .cpp file and get it to work with my existing main().

I tried to follow [URL] which is a closed article, with no success. I tried every combination I could think of and was unable to compile without error. (Linux Mint 17,code::blocks 13.12, G++ 4.8.2). I did finally get it to work by putting *all* my code in the .h file and #including the .h file (and nothing else) in the .cpp file. This is not how it's supposed to work.

This is unbelievable! I just tried this on another computer, same OS same version of Code::Blocks and G++.

View 2 Replies View Related

C/C++ :: Passing Var Assigned In A Class File To Functions Outside Of Int Main?

Dec 7, 2014

I'm trying to pass the value of an object created from a class file to a function outside of the "Int Main" function in the main.cpp file. I've successfully created the object, I just want to pass it to a void function but I'm getting the scope error below. I'm not sure how to correct. I'm not having much luck with research either (static variables?).

error: 'TicTacToe' was not declared in this scope

main.cpp
#include <iostream>
#include <cstdlib>
#include "Signature.h"
#include "Gameplay.h"
using namespace std;
// Initialize array
char square[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

[code]....

View 4 Replies View Related

C++ :: Splitting Template Class To Header H File And Implementation CPP File

Sep 12, 2014

What is the right syntax for implementing the .cpp of a template class?

Consider this LinkedList.h file:
Code: #include<iostream>
#include"Iterator.h"

template <class T>
class LinkedList {

[Code] ....

How should the implementation for the LinkedList constructor, for example, should look like in the LinkedList.cpp file?

I tried this:

Code: #include "LinkedList.h"
template <class T>
LinkedList<T>::LinkedList<T>() {
// constructor
}
LinkedList<T>::~LinkedList<T>() {
// destructor
}

But the compiler wouldn't accept it.

View 4 Replies View Related

C++ :: How To Declare Class As Forward In Header File - Regular Use In CPP File

Jan 17, 2015

lets say we have a valid class with full implementation, X. can this class be declared as forward in Y header file:

Code: class X;

class Y{
X* m_X;
}

but still be used as regular in the cpp file?

Code:

#include "Y.h"
#incldue "X.h"
T Y::function(){
m_X->doSomething();
}

visual studio prevents me from doing it , I wonder if the standard also says so.

View 2 Replies View Related

C/C++ :: Declaration Of Class In Header File

Sep 24, 2014

In the code given below using turboc++ 3.0(really old i get it but its school rules)

#ifndef emoji_h
#define emoji_h
class emoji//here for some reason {
};
#endif

error i get :line 3 declaration syntax error why and how to correct it?

View 1 Replies View Related







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