C++ :: Creating A Variable Inside Function Arguments

May 15, 2013

In the following code:

#include <iostream> // For stream I/O
using namespace std;
int function(int a) {
return a;
}
int main() {
function(int b);
}

Why is creating a variable inside the function argument list not allowed. Any reason other then for the language syntax or just for the language syntax?

View 19 Replies


ADVERTISEMENT

C :: Prototype For A Function Accepting Variable Arguments

Dec 6, 2014

what should be the prototype for the following function.

Code:void addition(int x, ...);

I am getting compilation errors. I have written the prototype as :

Code: void addition(int, va_list);

View 3 Replies View Related

C++ :: Static Variable Inside A Member Function

Jul 20, 2013

Say you had:

class Foo{
public:
//...
void funky();

[Code] .....

Would each instance of Foo create a new counter variable, or would it remain the same for all of them, i.e. baz.funky() would always use the same counter variable? What if the class was a template?

View 3 Replies View Related

C++ :: Undefined Reference Error When Accessing Static Variable Inside Member Function

Feb 10, 2013

I am modifying a set of static variables inside of the class's member function. The static variables are private. An example of what I'm doing is as below,

utilities.h
-----------
class utilities {
private:
static int num_nodes;

public:
void parse_details(char* );

[Code] ....

I get a compilation error in the function void utilities::parse_details(char* filename)

which says: undefined reference to `utilities::num_nodes'

compiler: g++

View 2 Replies View Related

C :: Using Or Inside Case Arguments

Nov 6, 2013

I would like to choose same case for multiple switch conditions.

For example:
Code: switch(choice) //''if choice ==0 or choice ==1, chose same case''
{
case (0 || 1):
{
//execute steps
}
break;
default:
{
//execute steps
}
break;
}

The '||' inside case does not have the desired effect (although it compiles fine). How can I do it without using if-else statements.

View 1 Replies View Related

Visual C++ :: Creating A Variable Using A Variable?

Jun 16, 2013

Any way to create a variable using a variable in the name? So E.g. if you wanted to create an int named nr(x), and x was 1, you would get an int variable named nr1? How would you do this?

View 10 Replies View Related

C++ :: Functions With Variable Number Of Arguments

Apr 21, 2014

Write a function write with variable number of arguments that takes a string first argument followed by any number of arguments of type double and prints on the screen a string formatted by the rules described below. The first argument may contain formats in curly braces of the form {index[:specifier]}, where the square brackets show optional parts (this is :specifier may be missing), and index is the sequence number of an argument of type double (starting from sequence number 0).

Rules for formatting: In the printed string the curly brackets and their content will be replaced by the argument with the given index, formatted according to the given format specifier. If the format specifier is missing, the argument will be printed with its default format. For example:

write("The number {0} is greater than {1}.", 5, -3);
will print
The number 5 is greater than -3.

write("There are no format specifiers here.");
will print
There are no format specifiers here.

The format specifiers and their meanings are listed in the following table

Specifier MeaningFormat Output for 1.62 Output for 2.0
none default {0}1.62 2
ccurrency{0:c}$1.62 $2.00
escientific{0:e}1.620000e+000 2.000000e+000
ffixed point{0:f}1.620000 2.000000
iround to int{0:i}2 2

Limitations: You may limit the maximum number of arguments your function can process to a certain value, for example 10.

Suggested extensions:
-Add an optional alignment specification in the format , e.g., make the format of the form {index[,alignment][:specifier]}, where alignment is an integer specifying the width of the field in which the corresponding argument will be printed. If alignment is positive, align to the right, if it is negative, align to the left.
-Accept an optional integer after the specifier letter, specifying the required precision in the output. For example, {0:f2} will print the number 1.6234 as 1.62, but {0:f5} will print it as 1.62340.

View 1 Replies View Related

C :: Dynamic String Using Variable Number Of Arguments

Mar 6, 2015

I need to create dynamic string by given format(%d,%s,%f,%lf,%c) using variable number of arguments in function. This code gives me an error(main.exe has stopped working):

Code:

#include<stdio.h>
#include<stdarg.h>
#include<string.h>
#include<stdlib.h>
char *form(char *format,...);
char *form(char *format,...)

[Code]...

I assume the error is in functions(itoa,fcvt,ecvt).

View 1 Replies View Related

C/C++ :: Dynamic String Using Variable Number Of Arguments

Mar 14, 2015

I need to create dynamic string by given format(%d,%s,%f,%lf,%c) using variable number of arguments in function. This code gives me an error(main.exe has stopped working):

#include<stdio.h>
#include<stdarg.h>
#include<string.h>
#include<stdlib.h>
char *form(char *format,...);
char *form(char *format,...)

[Code] ....

I assume the error is in functions(itoa,fcvt,ecvt).

View 2 Replies View Related

C :: Set Struct Member Variable For Structure Inside Def

Mar 12, 2014

This is with Linux gcc

Code:
typedef struct _a
{
int id;
} a;
typedef struct _b
{
a my_a;
my_a.id = 1; // error: expected specifier-qualifier-list before "my_a"
} b;

I get error: expected specifier-qualifier-list before "my_a"

I must set the id for the kind of struct created inside the struct def because main() will be casting based on this id. Thats how I will know which structure b contains by it's id, there could be hundards of different structs with different values I will cast to the correct one and know it's members by it's id. How do I ?

View 10 Replies View Related

C++ :: Difference Between Variable Declaration Inside And Outside For Loop

Sep 10, 2013

What is difference (memory allocation or any) between declaring a variable inside or outside the variable

program1:

#include<stdio.h>
int main() {
int i;
for (i=0;i=<100;i++) {

[Code] .....

Difference b/w program1 and program2. Its look both are same. but my compiler shows something difference.

View 1 Replies View Related

C++ :: Add Variable Address To Void Pointer Inside Of Class?

Dec 1, 2013

How can I add the variable adress to a void pointer inside of a class?

class variant2 {
private:
void *Vvariant=NULL;
public:
template<typename b>
variant & operator = (b *adress)

[Code] ....

if possible i want avoid the '&' when i assign the variable address.(variant2 f=varname;//like you see i don't use the '&')
for the moment i just need put the address to Variant pointer. but i receive several errors .

View 4 Replies View Related

C++ :: Variable Inside A Class - How To Manage Memory Allocation

Jul 30, 2014

recently I developed a class header only in C++ to deal with byte arrays. Since I developed programs in other languages that had some libraries to dial with byte arrays, I developed one that the syntax is very similar to other languages.

Since I'm not very familiar with memory management, I was concerned about somethings I've read about (ex: memory fragmentation).

Here is a very simple example of my practice:

class ByteArray {
public:
ByteArray(size_t size) {
buffer = (int8_t*)malloc(size);
}

[Code].....

The class is intended to be used as part of comunication protocol in a webserver, byte arrays are created and destroyed a lot. Should I use pools? Is there a better practice? Am I doing everything wrong (laugh)?

For those who wants to see the entire class: [URL]

View 9 Replies View Related

C/C++ :: Difference Between Declaring Static Variable Inside And Outside Of Main

Apr 6, 2012

I want to know

prog1.c
#include<stdio.c>
static int c=6;
int main() {
/*code*/
}

prog2.c
#include<stdio.h>
int main(){
static int c=10;
}

what would be the difference between these two program in the fuctioning of static keyword ?

View 1 Replies View Related

C++ :: Creating A Class Variable With A String?

May 12, 2013

I have defined a class in a header file; just the class, no templates involved. I have a program where I'm reading in data in string format. Each string consists of a word, a delimiter, and a variable name. Example:

cajun/mustard

I want to take that string and make it the variable name of that class type. It would be implemented along the lines of:

Code:
string str;
//read/process string here, get:
str = "mustard";
createName(str);
//pass string to creator function When the function is called, I should get the variable:
Class mustard;

Thing is, I'm not supposed to know beforehand what the variable names are, only that I create them as they are read in. It could be mustard, it could be Maynard_James_Keenan, it could even be bazinga.

My problem is, what do I do for createName()? I've looked into the concepts of pairing, Factory implementation, and maps, but I don't think they answer my question.

(P.S. if I run into the same variable name being read in twice, what steps can I take to make sure that a duplicate variable isn't created? Do I need to add in code, or does the compiler know to watch for multiple variables of the same name?)

View 6 Replies View Related

C :: Creating Variable Length Arrays

Oct 24, 2014

How do you prompt the user to enter the number of elements for the array and use that information to creatr a variable length array? And then how do you prompt the user to enter in a number for each element of the array and scan in the appropriate numbers? the numbers are double precision floating point.

for example,
Enter the numbe of elements in the array: 3
Enter element 0: 3
Enter element 1: -1
Enter element 2: 4

I know it starts with

int main() {
double N;
int a[size];

printf("Enter the number of elements in the array:" );
scanf("%f", &size);

//I'm pretty sure this is wrong

View 8 Replies View Related

C++ :: Creating Variable Name For Binary File

Dec 11, 2013

I'm attempting to split a large binary file into smaller manageable files for analysis. I've written most of the software but I'm stuck in a couple of places.

1. The binary file is split by looking at a couple of bytes to determine when to create a new file or continue appending to the current new file. The question is when I need to create a new file, how can I dynamically sign it a name? My intention is to rename each subfile by: "original_name" + new section id + ".log".

2. The start of each section is determined by a specific pattern (6 bytes of FF's). I'm running into an issue where the pattern check is checking for 5 bytes instead of 6 because the for..loop doesn't increment for one instance.

I've attached the code I have so far.

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <sstream>
using namespace std;

int append_to_file(FILE *f,long sec_start, long sec_end)

[Code] ...

View 2 Replies View Related

C++ :: Creating Global Variable / Pointers?

Jan 6, 2012

I am making two classes using the juce library [URL] ....

this is a problem that has now come up after solving an earlier problem i posted but its a completely seperate issue.

i have a class for loading a file when i button is clicked that seems to work. Here is the function in my LoadButton cpp:

void LoadButton::buttonClicked(Button* button)
{
FileChooser pedoFileChooser ("Choose an Audio File", File::nonexistent, String::empty, true);
if (pedoFileChooser.browseForFileToOpen())
{
File pedoFile (pedoFileChooser.getResult());
}
}

I want to be able to then use the file stored in pedoFile in the cpp of another class called PlayButton. I tried doing this with a pointer? not sure if that's correct way of doing it (i know very little about C++ or programming) by changing the function to this. I'm getting the error invalid initialisation of non-const reference of type 'juce::File*&' from a temporary of type 'juce::File'

void LoadButton::buttonClicked(Button* button)
{
FileChooser pedoFileChooser ("Choose an Audio File", File::nonexistent, String::empty, true);
if (pedoFileChooser.browseForFileToOpen())
{
File* &pedoFile = (pedoFileChooser.getResult());
}
}

View 3 Replies View Related

C++ :: How To Pass Address Without Creating Local Variable

May 14, 2012

I am doing a piece of gui code on some embedded system.

I'm trying to find a way of eliminating the local variable kEvent:

Code:
EVENT kEvent;
....

Code:
kEvent = EVENT_UPSTREAM;
xStatus = xQueueSendToBack(getEventProcessorQueue(),&kEvent, 0 );
....

I tried this, it doesn't work:

Code:
xStatus = xQueueSendToBack(getEventProcessorQueue(),(EVENT *)EVENT_UPSTREAM, 0 );

Shouldn't this work?

View 1 Replies View Related

C++ :: Creating Variable On Runtime - Message Decoding

Apr 10, 2013

I receive messages over a bus. Suppose there is a function to read the messages storing them into a struct defined as follows:

typedef struct
{
ULONG timestamp;
BYTE ID;
BYTE data_length;
BYTE data[8];
} MSG_FRAME;

Depending on the message ID different messages represent different values for one project.For example msg with ID 10 can include in the 8 bytes something like:

width: 6 bits
height: 6 bits
actpos: 12 bits
maxpos: 12 bits
minpos: 12 bits
range: 16 bits
total: 64 bits = 8 bytes

Printing the message is no big deal. But here comes the tricky part. I want to print out the specific information hidden in the 8 bytes. I can define the structures for every msg ID and compile the program with this "special" header file, but I want to do it during runtime of the program, loading the information regarding the msgs, because i can have different projects where the information for different msg IDs can differ.

I've a non-C file, where basically all the information is written. Lets stay frame named

GetStatus{
bit 0 - 7 width
bit 8 - 15 height
.
.
}
etc.

How to read it on runtime and decode the messages? On runtime I'm not able to create variables and structures anymore!

View 13 Replies View Related

C++ :: Creating Series Of Outputs Using Array Created By Variable?

May 18, 2014

I'm expected to get a starting minimum input, and also an ending maximum output (for example: 21, and 25). From here, i have to give output using all the numbers (in a row) between the min and max numbers.

(for the same example:
21
22
23
24
25)

I assumed I would want to create an array using a variable, but i'm not sure of that either.

View 4 Replies View Related

C++ :: Defining Constant And Creating Double Variable With Initial Value?

Apr 4, 2014

Define a constant PI that has a value of 3.14159

- Create a double variable, Radius with an initial value of 10

- Create two double variables. Circum and Area, without initialization

- Using the following formulas, compute circumference and area of the circle:

circumference = pi * r * 2 (here, r means radius)

area = pi * r * r

- Display the result using three variables (numbers must come from variables)

- Expected output: (Don’t forget to display the period at the end of the first line)

Circle with radius of 10.

Circumference = 62.8318 Area = 314.159

View 3 Replies View Related

C++ :: Using Template Function Inside Class In Separate Function?

Mar 26, 2014

i want to use a class to print data stored as vector or array with different data types. i also want the print function two take more than one vector or array or combination of both so that they can be written to file as two columns. so i wrote the following class:

right now it has only one member function for printing two vectors. later i'll add additional functions as required.

note: there has to be template functions inside the class
i also want the object to be global so that i need not pass it as an argument to other calling functions

class printdata
{
public:
template<typename T1,typename T2>
void SaveData( vector<T1> &data1,vector<T2> &data2, std::string var)
{

[Code]....

then i want to call this template function in another ordinary function written in a seperate cpp file

these function declarations are put in a header file. so i need know whether i should put the declaration of the template function in the header to use the function in different functions

View 4 Replies View Related

C/C++ :: Using Template Function Inside A Class In Separate Function?

Mar 26, 2014

i want to use a class to print data stored as vector or array with different data types.

i also want the print function two take more than one vector or array or combination of both so that they can be written to file as two columns.so i wrote the following class:

right now it has only one member function for printing two vectors. later i'll add additional functions as required.

note: there has to be template functions inside the class / i also want the object to be global so that i need not pass it as an argument to other calling functions

class printdata {
public:
template<typename T1,typename T2>
void SaveData( vector<T1> &data1,vector<T2> &data2, std::string var){
std::ofstream myfile;
std::string filename;

[code].....

then i want to call this template function in another ordinary function written in a seperate cpp file these function declarations are put in a header file. so i need know whether i should put the declaration of the template function in the header to use the function in different functions.

View 1 Replies View Related

C++ :: Creating Priority Queue Of Objects Transaction As A Variable Of Store Class

Dec 4, 2013

I've got 2 classes, Store and Transaction and I would like to create a priority queue of objects Transaction as a variable of Store class.

My store.h
#ifndef __STORE_H_INCLUDED__
#define __STORE_H_INCLUDED__
#include <queue>
using namespace std;
#include "Transaction.h"
struct TransactionCompare;

[Code] ....

The error im getting with this set up is

error C2664: 'bool TransactionCompare::operator ()(const Transaction &,const Transaction &)
const' : cannot convert parameter 1 from 'Transaction *' to 'const Transaction &'

View 6 Replies View Related

C++ :: Function Does Not Take 3 Arguments

Apr 27, 2013

I am doing a problem where I need to use arrays of string objects that hold 5 student names, an array of one character to hold the five students' letter grades and five arrays of doubles to hold each student's set of test scores and average score.

When I try to run it, I get these five errors.

error C2660: 'getTestScore' : function does not take 3 arguments : line 39
error C2660: 'getTestScore' : function does not take 3 arguments : line 45
error C2660: 'getTestScore' : function does not take 3 arguments : line 51
error C2660: 'getTestScore' : function does not take 3 arguments : line 57
error C2660: 'getTestScore' : function does not take 3 arguments : line 63

what is wrong.

Here's my code.

View 4 Replies View Related







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