C++ :: Why Can't Access The Variable
Jul 17, 2014
Code:
#include <iostream>
using namespace std;
void f();
extern int x;
int main() {
[Code] .....
x is declared outside the functions and defined inside main(). Then why this code produces a compile error?
x is already declared so it can be used in f(); and when I call f(), x is already defined. Then why can't f() sets the value of x (in main) to 10?
View 3 Replies
ADVERTISEMENT
Oct 22, 2014
how can I make the queue of type pcb
and how to access pid in other clases?
View 6 Replies
View Related
Sep 7, 2013
I was trying out programs based on extern and as i understand, this is useful when accessing variables across multiple files having only one definition. But i tried a simple program as below without "extern" and thing seem to work when i expected it would fail during linking process
Code:
file5.c
1 #include <stdio.h>
2 #include "var.h"
3
4 int main() {
5
6 printf("
File5.c a = %d", a);
[Code] .....
As i have included "var.h" in all header files without extern, "int a" would be included in both the .c file and during linking, compiler should have thrown a warning or error message but it compiles file without any issue. Shouldn't var.h have the following "extern int a"?
View 8 Replies
View Related
Apr 3, 2013
I've created a class called Voter with a private member variable ID, also I have a variable in my main function to be ID as well. I'm trying to compare the two ID's but when I do so:
if (ID == V.ID)
I get the error - 'std::string Voter::ID' is private within this context.
I know that because it's private I can't access it, but how do I?
View 3 Replies
View Related
Sep 2, 2013
I have 3 files: f1.c f2.c and f3.c
I have declared variable int a; in f1.c and int a; in f2.c.
Now I want to use the variable int a; in f3.c . But this variable corresponds to the variable in f2.c .
View 4 Replies
View Related
Dec 18, 2012
I have two cpp files.I m updating a value of an integer in one cpp file and i want to perform a specific function in the second file as per to the value updated in the first file. ie I m initialing the variable in the constructor. I have two buttons in the first cpp file. On clicking the first button I m updating the variable as 1 and on pressing button two , I m updating the variable as 2. I m retrieving this variable in the second cpp file with respect to the object created for the class in the first cpp file.
The problem is that i m not able tot retrieve the updated value in the second file. only the initialized value is being retrieved. neither 1 nor 2 is updated.
View 3 Replies
View Related
Aug 8, 2013
Code:
#include <iostream>
using namespace std;
void f() {
int x=17;
//cout<<main::y<<endl; i want to access y from main scope
}
int main() {
int y=23;
//cout<<f::x<<endl;
I want to access x from f scope is there any way for this without global declaration? specially about function scopes...
View 1 Replies
View Related
Nov 9, 2013
I am trying to access a variable from another class through another class but it is not returning its "instance"?
Example:
Class View
Code:
...
V3D_Viewer viewer;
...
Class MainWindow
Code:
...
viewer* myView;
myView = new viewer();
...
Class Test
Code:
...
MainWindow window;
window.myView->setBackgroundColor(WHITE);
...
I am new to c++ references and pointers,
View 3 Replies
View Related
Sep 9, 2013
if we don't provide the acces modifiers for base class and we need to manipulate the private data of base class in derived class. Is there anyway to acces the private data members? Here's a coding example
class A {
private :
int a;
};
class B : public class A {
public :
void displayA() { cout<<a<<endl; }
};
how i can acces the a of base class A in derived class B without acces modifiers.
View 16 Replies
View Related
May 10, 2013
I would like to convert a float or double variable into unsigned char variable and back.
float number_float = 23.453f;
unsigned char* number_char = (unsigned char*)malloc(sizeof(float));
number_char = reinterpret_cast<unsigned char*> (&number_float);
float* number_float_0 = reinterpret_cast<float*>(&number_char);
I am not getting the same value back.. why?
View 2 Replies
View Related
May 1, 2013
I am having trouble compiling my interface. I am trying to store a reference variable as a member variable of the interface object. Compiler says that the variable has not be initiated correctly.
LCD inherits from VisualInterface which is expecting a DisplayDriver object to be passed in (DisplayDriver is another interface, but thats not important).
I pass the displayDriver object in when LCD is instantiated in maininterfaces.zip
I was pasing it before as a pointer but was told that this could cause me problems with memory leaks and a reference was better, but now I cant seem to get it to compile.
View 11 Replies
View Related
Mar 2, 2013
This problem is best explained by looking at its output below. I stored 15 different values but when retrieved some are wrong.
#include <iostream>
using namespace std;
class A {
public:
int ** pointer;
[Code] ....
I need to store and retrieve the 15 different values correctly. How to solve this? The main thing is, I need to store the pointer in class A objects.
View 6 Replies
View Related
Sep 8, 2013
I have to write a loop assigning a variable x to all positions of a string variable and I'm stuck. I don't have extensive experience with arrays and I'm also a bit confused about C-String. The problem is below.
"Given the following declaration and initialization of the string variable, write a loop to assign 'X' to all positions of this string variable, keeping the length the same.
char our_string[15] = "Hi there!";
(Please note this is a 'C-string', not C++ standard string.)"
View 5 Replies
View Related
Oct 25, 2014
I need to transform a local variable into a global variable so I can use it in one of my functions. I thought about passing the value as a parameter to this function but I can do this since the function is called inside the while loop and this variable counts how many times the while loop does (so the final value is outside the loop). Example to visualize better:
Code:
while(condition) {
function(parameter1, parameter2);
count = count + 1;
}
printf("%d
", count);
So, I need to transform the final value of "count" into a global variable. Can I do this?
View 5 Replies
View Related
Oct 15, 2013
I want to store few different functions to a variable for different structs/classes and then call it later using that variable, is it possible? something like
struct item {
int ID;
int special; // for function
};
item Key;
Key.special = UseKey(KEY_KING);
// now when I want to call function "UseKey(KEY_KING)" I want to use "Key.special", like this
if(iCurrRoom == ROOM_KING)
Key.special;
else if(iCurrRoom == ROOM_DRAGON)
Fireball.special;
View 5 Replies
View Related
Feb 3, 2014
I've been experimenting with pointers and am getting the below error.
'error: cannot convert 'int**' to 'int*' in assignment'
I thought it was ok to assign a variable address to another variable. Line 18 is where I get the error.
I am trying to show the progression of memory as I increment it as I have done on line 17 and again, I don't know why I don't see a progression through memory locations when output to the console on line 20.
Here's the code:
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main() {
int *tt = new int;
int *p = new int;
[Code] .....
View 6 Replies
View Related
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
Aug 10, 2014
How to change an enum type variable to a string type variable?
View 2 Replies
View Related
Aug 5, 2013
Here is the code,
Code:
class A {
};
A& CreateObject() {
static A a;
return a;
} static A aa;
int main() {
return 0;
}
So is there any difference between a defined in CreateObject and aa?
View 6 Replies
View Related
Apr 9, 2012
this is the XML document i have:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
<ConfigParams>
<Param Name="1">1857</Param>
<Param Name="2">10.31.225.163</Param>
[Code] ....
How to write a C++ code to access thing. (like we use JAXB in java)
View 1 Replies
View Related
Aug 31, 2014
So I have a class object that contains the private member variable spot and the public member function MoveLock. Within MoveLock, is a member variable called numbers that holds the place where a user is on a "lock knob". Now, what I'm trying to accomplish is that whenever the user turns the "knob" in the wrong direction, the position is updated with that current numbers so that the clicks needed to unlock the first state is also updated. But I get these errors:
Error E2096 C:Users...switchtest.cpp 34: Illegal structure operation in function main()
Error E2294 C:Users...switchtest.cpp 39: Structure required on left side of . or .* in function main()
Ultimately, what I have in main() is a piece of what I'm going to implement in a class member function. I'm also thinking about moving the if else statements out of the for and creating a second one for the else portion.
#include <iostream>
#include <windows.h>
#include <iomanip>
using namespace std;
HANDLE inKeys = GetStdHandle(STD_INPUT_HANDLE);
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
[code]....
View 10 Replies
View Related
Jun 29, 2014
Is it possible to access file properties from c++ program? For example, user could drag file to program and then it displays a detailed information about file like date modified, size, type and etc..
View 1 Replies
View Related
Sep 8, 2014
How to access the name and file name members on line 42 and 43?
Code:
typedef struct {
char * name;
char * filename;
} FILES;
FILES * files[256];
}
[code]...
How to access the name and filename from within function?
*files[count].name = chTmp;
View 9 Replies
View Related
Jul 31, 2013
I have an array called abee1.
int abee1[4][3];
I have done some string function (like: "abee" + "1" ) and have "abee1" as string which is the same as abee1. How can I copy the data of abee1 into another array with same size, for example abeeTwo[4][3], using only the name of abbe1 as string ("abee1") not by abee1 directly.
View 4 Replies
View Related
Jun 25, 2013
I have the following code below. I am getting a memory access violation when accessing cmd->query_string in the loop function. The loop() function is running in another thread. The cmd object appears to be destroyed after calling the send_command function. How do I create an object on the heap and access the query_string.
std::list<my_command_message_que*> my_command_que;
void loop(){
if(my_command_que.size() > 0){
my_command_message_que * cmd = my_command_que.front();
std::cout << cmd->query_string;
[Code] ....
View 2 Replies
View Related
Jun 25, 2013
On linux, I can compile DLLs (shared objects) as well as executables that use them. I can even access globals and classes that are defined in the EXE from the DLL, and vice versa, simply with the 'export' keyword. flawlessly.
The Problem: But on Windows (using MinGW), no matter what I do, I'm completely unable to access global variables which defined in the EXE, from the DLL. On Linux, this is no sweat, but what's Windows' problem?
I also need to extend classes in the dll with base class method definitions defined in the exe.
Ive heard that on Windows, you need to use declspec(dllimport) and declspec(dllexport). I can compile with CygWin+MinGW/g++4.5.3 as well as "Pure Windows" with MinGW/g++4.7.2 *without* the declspecs. So what's the decljunk for? Is this really just something for MSVC or other compilers?
Here's some Windows code to show what the problem is. The DLL's global variable is accessible to the EXE just fine, but the EXE's global variable is not accessible to the DLL - compilation complains it is an undefined reference.
main.cpp
#include "myLib.h"
#include <stdio.h>
int exe;
[Code].....
edit: I tried using --enable-runtime-pseudo-reloc --allow-shlib-undefined options when compiling the DLL and G++ complains that --allow-shlib-undefined is an unrecognized option.
View 1 Replies
View Related