C/C++ :: Multiplying Two Member Of A Class?
May 1, 2014
we have to make a Invoice class which has a function called computeInvoiceAmount() which multiplies the price and the quantitiy which are private member of the class.
double computeInvoiceAmount(){
return quantity * pricePerItem;
}
This is my function, but if i compile it i get the following error:
"Invoice.cpp:53:11: error: "quantity" was not declared in this scope" and "Invoice.cpp:53:22: error: "pricePerItem" was not declared in this scope"
I tried it with the get functions but i get the same error.
Full code is here: [URL]
View 1 Replies
ADVERTISEMENT
Oct 7, 2014
How to initialize a static member of a class with template, which type is related to a nested class?
This code works (without nested class):
#include<iostream>
using namespace std;
struct B{
B(){cout<<"here"<<endl;}
};
template<typename Z>
[Code] ,....
View 1 Replies
View Related
Aug 21, 2013
I am writing a program which is using SDL library. I have two different classes which one of them is Timer Class and the other is EventHandling Class.
I need to use some member functions and variables of Timer in some Eventhandling Class member functions, Although I want to define an object of Timer in int main {} and relate it to its member function that has been used in Eventhandling member function in order that it becomes easier to handle it, I mean that I want to have for example two objects of timer and two objects of Eventhandling class for two different users.
I do not know how to relate an object of a class from int main{} to its member function which is being used in another class member function.
Lets have it as a sample code:
class Timer {
private:
int x;
public:
Timer();
get_X();
start_X();
[Code] ....
View 4 Replies
View Related
Sep 4, 2014
So I have an ImageManager class, Board class, and Box class. In Board.h I can declare ImageManager imgr; and in Board's constructor I can use imgr and its functions and such. However, in Box.h when I try and declare ImageManager imgr; I get the error "cannot access member declared in class ImageManager". Both declarations are under private, and exactly the same, but one doesn't work. Also, is there a way to only have one instance of ImageManager?
View 19 Replies
View Related
Jun 26, 2014
I am having compiling issues and am looking for an explanation as to what is causing the error and how to fix it. The declaration of 'g4vuplInstanceID' seems to be global in scope in my option, however I may be wrong.
Compiler Error: error: 'class myPhysListGeneral' has no member named 'g4vupInstanceID'
#include "G4VUserPhysicsList.hh"
#include "G4VUPLSplitter.hh"
#include "G4VPhysicsConstructor.hh"
#include "myPhysListGeneral.hh"
#include "G4ParticleDefinition.hh"
#include "G4ProcessManager.hh"
#include "G4ParticleTable.hh"
[code]....
View 19 Replies
View Related
Sep 18, 2013
How can a member function in my derived class call the same function from its base class?
View 1 Replies
View Related
Feb 23, 2014
I'm very new to C++ so I've been trying to run through some code examples to begin to learn basic structures and syntax, but I've recently run into a problem using examples from the 7th ed. of Sams Teach Yourself C++. I'm using the code provided within one of the examples that allows you to specify and multiply two variables, but when I compile and run the executable the final output seems to only show the first variable and b/c of this the multiplication operation does not work.
Here is a my example code:
Code:
#include <iostream>
using namespace std;
int main() {
cout << "This program will multiply two numbers" << endl;
[Code] ....
View 3 Replies
View Related
Apr 19, 2014
#include <iostream>
#include <cstdlib>
using namespace std;
[Code]....
I am trying to initialize int x[] = {10, 100, 1000, 10000} into a loop and give me four different numbers for something. I was trying to get creative and just do
myArr[j] = j * 10; with (int j = 1; j < 10000; j++) ,
But that doesn't work. It still gives me an output of 10 11 12... How to make an array punch through a loop and give me
10 Press any key to continue...
100 Press any key to continue...
1000 Press any key to continue...
10000 Press any key to continue....
I'm thinking I must set the for loop to < 10000.1 or < 10001 to output the final value in the array.
View 3 Replies
View Related
Oct 21, 2013
I mount a function (parameter - numeric vector; returns a string). However, this same function is used in several classes. To avoid that I keep duplicating the same code within these classes there is a way to do that as the code below?
std::string func( const vector<int> vec ) {
//processamento
return result;
} class A {
[Code] ....
View 6 Replies
View Related
Jun 16, 2013
whether i can define a member function inside a class or not in C++. Is the following code is legal?
#include<iostream> using namespace std;
class adder {
private:
int a;
int b;
int c;
int answer;
public:
[code]....
View 6 Replies
View Related
Jan 18, 2013
I am looking to have a member of type bitset in a C++ class. Is it possible ?
Something like below:
Class abc
{
bitset<32> var;
}
View 4 Replies
View Related
Sep 27, 2013
If you are doing some big program, usually, how do you organize the files? Put the class and its member in head file, but where to declare non member functions and where to define them? I don't want to put them all in one cpp file. If not, how to make them visible to the main cpp file?
View 4 Replies
View Related
Jun 3, 2013
Trying to multiply to matrixes using the following algorithm,
Code:
ABrec(A,B)
n=A.rows; //n must be multiple of 2
C is a new n*n matrix.
if(n==1)
C[0][0]=A[0][0]*B[0][0];
[Code] ....
but the code doesn't work !!!
View 4 Replies
View Related
Nov 16, 2013
I have an array as such and need to multiply all the values of it by each other and put that as a variable.
//for example
int array1[100]
//assume i have already input the values of each element properly up to, say, element 4
//I need to now multiply all of those values together. I think I need the size of the array to control what it multiplies (I know how to find size) ....
View 4 Replies
View Related
May 14, 2012
Two different matrices will be read from text files (input1.txt, input2.txt) and they will be stored in two dimensional arrays (matrix1, matrix2) and one dimensional arrays (array1, array2). Our aim is to obtain the matrix multiplication using two dimensional and one dimensional arrays.
You are asked to write the main and the following functions. The definitions of the functions are given in the skeleton code.
int read_file(ifstream& in_file, int &row, int &col, double *array, double **matrix)
int write_file(ofstream& out_file, int row, int col, double **matrix)
void print_matrix(double **matrix, int row, int col)
void print_array(double *array, int row, int col)
void multip(double **matrix1, double **matrix2, double **result, int k, int m, int n)
void multip_array(double *array1, double *array2, double *array_result, int k, int m, int n)
You are going to obtain the input and output files as command line arguments
input1.txt :
3 4
2 3 4 5
1 3 5 4
0 4 4 7
The first element in the first line represents the number of rows (3) and the second element represents the number of columns (4) of the matrix (the result file will have the same format).
likewise;
input2.txt :
4 5
1 2 3 4 5
2 9 43 44 21
32 32 32 43 54
1 3 3 4 5
thats my homework and here is the code i wrote from the sceleton code they gave me :
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int read_file(ifstream& in_file, int &row, int &col,/* double *array,*/ double **matrix)
[Code] ....
I didn't understand the array part but even when i exclude the array part i get the program has stopped working message. My os is windows7 ultimate with mingw installed and i compile the program using g++ command in cmd with the arguments input.txt input2.txt resultt.txt
View 1 Replies
View Related
Jan 10, 2015
I have the following scenario :
Code:
class test {
public :
int num;
};
int main() {
test t1;
test *ptr = &t1;
int test :: *mem_ptr = &test::num;
}
I need to access mem_ptr (pointer to a class member) through :
a. object (t1)
b. pointer to the object (ptr).
How can i do that ?
View 4 Replies
View Related
Apr 15, 2014
I want to have a template function that is a member of a class. Is this possible? This code snippet is how I would think the syntax would go, although it doesn't compile. How would I achieve the same effect?
Code:
class myclass {
public:
int member ;
} ;
template <typename T> void myclass::func( T& arg )
[Code] .....
View 4 Replies
View Related
Feb 16, 2013
I get a problem with the vector as a private class member: When I did't initialize the vector in constructor(which means the size of the vector would be 0), I used a class function to add two elements to the vector and it worked (because I added a "printf" to output the size of the vector and the elements within that function). However, when I used another class function to visit that vector, no element was in and the size became 0.
Then I tried to add two elements to the vector during the construction, and it turned out that these two elements could be stored in the vector while other elements added through class functions could not.
I guess there may be some problems on the scope of the function. But I feel the class member should not be effected by the scope of the class function.
View 7 Replies
View Related
Oct 24, 2013
This question is more from a design point-of-view rather than coding it to be a fully functional.
So here it goes:
I have multiple files which each require their own object of same class type (ref. First Class). File contents are read from a file to a unordered_map<std::string, std::vector<std::string>> which is either private or protected member inside First Class. First Class does not need any public functions to add, remove or change the data during runtime, but changes are only being made by checking if the file size has changed during the day, if the size is not equal to the last check, map gets updated.
Now, I have a Second Class which is a data handler class. It has public member functions with arguments that needs to be get from First Class's unordered_map using const_iterator. Which way to go with design and implementation.
I know there's two methods to do this. Re-doing handler class is also not out of the question. These two methods I'm aware of are:
1. Declare these maps to local scope, build few global functions and here we go. (Probably the easiest way.)
2. Create public member functions to a First Class which either return a pointer or a reference to a protected/private member. (I'm under the impression that I really shouldn't be doing this because of a bad coding practice.)
Note that I don't need any code here, just some other point-of-views regarding the subject itself for learning better coding practices.
View 5 Replies
View Related
Jun 27, 2013
Basically, I need to set a variable outside of the constructor and make it accessible to the entire class.
It would need to work something like this:
#include <iostream>
#include <string>
template <typename MT> class CallbackFunction
{
[Code].....
View 5 Replies
View Related
Jul 5, 2013
I have some doubt regarding class data member accessing in another file.Follwing code showing error.
class A://file a.cpp
{
public:
int add;
int sub;
};
//file b.cpp
extern class A
void cal()
{
A::add=A::sub;
}
View 4 Replies
View Related
Aug 18, 2013
can we make a class without any data member ? but it may have member functions ! in c++
View 2 Replies
View Related
Dec 18, 2013
Is it possible to initialize class member variables in their definition statement instead of using a constructor?
Is the code bellow correct?
class rectangle
{
float a=0;
float b=0;
public:
string color="Red";
...
};
Which C++ Standard allows it?
View 2 Replies
View Related
Apr 9, 2014
I have encountered following lines in base class and I do not comprehend its meaning of "= 0" at the end of the member functions;
distance_list intersect(ray & r) = 0;
appearance get_appearance(vector & pt) = 0;
where distance_list is a list of doubles and appearance is properties.
In general, what does this "equal sign and 0 " mean for the member functions in the base class?
View 3 Replies
View Related
Jan 2, 2015
I made this code (it does nothing I am just learning about classes, I was learning about friend functions) and I don't understand what is wrong, here is the code:
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
class MyClass {
public:
friend int add(int a, int B)/>;
[Code] ....
I know i didn't need to include cstdlib and cstring for this code but as I said, it's not supposed to be something it's just for practice and I was working on char arrays. My question is about the part where i try to define the function:
int MyClass::add(int a, int B)/>
{}
My compiler(Microsoft Visual C++ 2010 Express) says that class MyClass has no member "add" even though it does...
View 3 Replies
View Related
Dec 30, 2013
If I want a class with a vector data member, can I specify it as follows?
std::vector< bool > integers( 101 )
I'm having some problems when compiling code.
View 3 Replies
View Related