C/C++ :: Structures Declaration And Definition
Dec 17, 2013is possible to combine structures declaration and definition in programming language?
View 3 Repliesis possible to combine structures declaration and definition in programming language?
View 3 RepliesI read that Memory is allocated during definition of a variable and not during declaration. Declaration is something like,
Code: int x;
And definition is assigning some value to it. This is what my professor taught. My doubt is if memory is not allocated during declaration, then how the compiler successfully compiles and runs the following, which i had already tried.
Code:
#include<stdio.h>
#include<conio.h>
int main() {
int c;
int *p=&c;
printf("%x",p);
getch();
return 0;
}
The variable c is only declared. But the program outputs a memory address. Shouldn't it show an error?
Why I take warning on this code :
Code:
#include<stdio.h>
extern int v = 0; // Declaration and definition of an external
int main(void)
[code]...
Is there any error? Why I take a warning: 'v' initialized and declared 'extern'|||=== Build finished: 0 errors, 1 warnings ===| ???
and what is the meaning of :An extern declaration that initializes a variable serves as a definition of the variable. This rule prevents multiple extern declarations from initializing a variable in different ways.
i am using c language to program PIC micro controllers, i am making a multi compilation unit project in order to organize my code better.
I want to create an array of functions and be able to call it from anyplace in the code
what i have done so far gplib.c
Code:
typedef void (*out)(int8);
void OUT_A(int8 weight){output_A(weight);}
void OUT_B(int8 weight){output_B(weight);}
void OUT_C(int8 weight){output_C(weight);}
void OUT_D(int8 weight){output_D(weight);}
void OUT_E(int8 weight){output_E(weight);}
out output_port[5+1] = {OUT_A, OUT_B, OUT_C, OUT_D, OUT_E};
how to declare them in gplib.h and how to call them anywhere in the code.
I am just learning using class template but I keep getting error unable to match function definition to an existing declaration
template <typename T>
class Homework {
private:
string name;
public:
template <typename T>
void SetName(T val);
[Code] ....
I've written the following code and keep getting the errors:
Error1error C2244: 'Supermarket<temp>::operator =' : unable to match function definition to an existing declaration
Error2error C2244: 'Supermarket<temp>::setName' : unable to match function definition to an existing declaration
Error3error C2244: 'Supermarket<temp>::setArea' : unable to match function definition to an existing declaration
#ifndef SUPERMARKET_H
#define SUPERMARKET_H
#include<string>
#include<iostream>
using namespace std;
[Code] .....
I moved the files to the .h file, and now I'm getting
Error2error LNK1120: 1 unresolved externals
I would like to write a program that prompts the user to enter a series of client names which the program will then store in an array of structures. I'm thinking something along the lines of:
struct sInfo {
vector<string> vName;
float fClientHoldings;
};
sInfo sClientData[100]; //create an array of structures for up to 100 client's
sClientData[0].vName="Acme"; //these will be inputted by the user in my program, but I've attempted to initialize them here for simplification
sClientData[1].vName="Enron";
I can't declare a structure array after I declare it. Is this true? If so, is there an alternate approach that I should be using?
I have an issue converting VC++6 code to VC++ 2010. The following template function definition is not allowed by the new compiler:
template <> void AFXAPI DelElems <CBrush*> ( CPen** objects, int count ) {
for ( int i = 0; i < count; i++, objects++ )
if (*objects)
delete *objects;
}
All errors refer to the header of the template function:
- syntax error : '<'
- syntax error : missing ';' before '<'
- 'DelElems' : illegal use of type 'void'
- unrecognizable template declaration/definition
I have a question about multiple definition of class LoaderException.
I have such code:
LevelLoader.hpp
#ifndef LEVELLOADER_HPP
#define LEVELLOADER_HPP
[Code]....
I found way around this, but I would like to know why it shows multiple declarations.
My solution is to declare this class in function body(void LoadLevel()), just before the throw statement. But why can't I define it inside my namespace, but outside function?
how to get this bug to go away and I am stressing about it. The project im working on simulates packet sending and receiving along with ROT13 encryption of the packets. The error lies within the .cpp file anywhere that I used IPHost.
header file
#ifndef IPHOST_H
#define IPHOST_H
#include <string>
#include <iostream>
class IPHost {
public:
//Default constructor defaults to 0.0.0.0
[code]...
error message:
In function 'ZStorSt13_Ios_OpenmodeS_':
line 11: multiple definition of 'IPHost::IPHost()'
line 11: first defined here
**this error repeats for every line in the .cpp in which IPHost is used.
What is the best way to define const strings when there are separate header and source files?
For example, I have a header that only declare some enums. In that same header I would like to add string representations of those enums so that I can print them easily i.e string_representation[my_enum] for debug and error printing and so on.
If I define them in the header file, I will get a linker error for multiple definitions. If I remove the definition, then I can not define it in the source file.
I have observed that inline functions can not be prototyped. example:
.cpp file:
inline void whatever() {
cout<< "this is inline"<< endl;
}
.h file, prototype
inline void whatever(); //would ask for a definition
Because of this, I have have just made functions that are used in only 1 .cpp file (ever) inlined, to make it more efficient (and it has demonstrated that it is more efficient). It's worked out fine so far, but what about the scope of the definition??
Since an inline function is like a templated function, in that it can't be prototyped, how are name conflicts resolved, and what is the best practice for writing inline functions??
Example of a conflict:
//in some arbitrary header...
void do_somthing();
//in .cpp file that inlcudes the header...
inline void do_somthing() {
cout<< "I'm doing somthing!!"<< endl;
} int main() {
do_somthing(); //which one?? it compiles fine though!!
return 0;
}
I have successfully built OGDF under directory undefined reference to /home/vijay13/Downloads/OGDF-snapshot/
I have following code in test.cpp under directory /home/vijay13/Downloads/ :
#include <ogdf/basic/Graph.h>
#include <ogdf/fileformats/GraphIO.h>
#include <ogdf/basic/graph_generators.h>
#include <ogdf/layered/DfsAcyclicSubgraph.h>
using namespace ogdf;
[Code] .....
while compiling as following :
vijay13@ubuntu:~/Downloads$ g++ -o test test.cpp -I /home/vijay13/Downloads/OGDF-snapshot/include/
I am getting following error:
vijay13@ubuntu:~/Downloads$ g++ -o test test.cpp -I /home/vijay13/Downloads/OGDF-snapshot/include/
/tmp/ccPE8nCu.o: In function `main':
test.cpp:(.text+0x26): undefined reference to `ogdf::Graph::Graph()' ...................... so on
I changed the name of my Invoice class to 'Application' and then it generated errors such as follows
Error9'Invoice.Invoice' does not contain a definition for 'Documents' and no extension method 'Documents' accepting a first argument of type 'Invoice.Invoice' could be found (are you missing a using directive or an assembly reference?)c:userskeildocumentsvisual studio 2013projectsinvoiceinvoicewritefile.cs1840Invoice
Error3'Invoice.Invoice' does not contain a definition for 'Run'C:UsersKeilDocumentsVisual Studio 2013ProjectsInvoiceInvoiceProgram.cs1921Invoice
I have added my classes here, lso I have added the sln to this post.
using System;
using System.Collections.Generic;
using System.ComponentModel;
[Code].....
I'm working on a personal project, where I have a database (SQLite) and a dataGridView. Now This is what I have:
public Form1() {
DataSet ds = new DataSet();
SqlConnection connection = new SqlConnection(@"Data Source=testingsql2.s3db");
SqlDataAdapter SQLda = new SqlDataAdapter("Select * from User", connection);
SqlCommandBuilder SQLcb = new SqlCommandBuilder(SQLda);
dataGridView1.Fill(ds, "User"); //Error here
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "User";
}
I've marked where I get the error.
And the Error description:
Error 1 'System.Windows.Forms.DataGridView' does not contain a definition for 'Fill' and no extension method
'Fill' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found
(are you missing a using directive or an assembly reference?)
I am unable to understand how a move constructor works in this example of code. If someone could break down the process of what is taking place and explain to me on why to use a move constructor.
Code:
class MyString {
MyString(MyString&& MoveSource) {
if( MoveSource.Buffer != NULL ) {
Buffer = MoveSource.Buffer; // take ownership i.e. 'move'
MoveSource.Buffer = NULL; // set the move source to NULL i.e. free it
}
}
};
Example from "SamsTeachYourself: C++ in One Hour a Day"
I keep getting following errors:
multiple definition of `SDL2Graphics::SDL2Start()'
undefined reference to `SDL2Graphics::SDL2Graphics()'
My set up is as follows:
Main.c++:
#include <iostream>
#include "GL/gl.h"
#include "GL/glu.h"
#include "SDL2graphics.c++"
/* Global variables */
int main(int argc, char* argv[]) {
[Code] ....
Is this standard-compliant code?
int f() {
class C {
public:
int mf() const {return 1;}
};
C c;
return c.mf();
}
Basically I have a text file called words. I'm supposed to extract a word randomly from the file and have the user guess the word according to the definition.
I'm having trouble matching the definition to the word from the text file.
Here's my code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
int number;
int count = 0;
string word;
[Code] ....
Here is my text file
apple#the usually round, red or yellow, edible fruit of a small tree
boat#a vessel for transport by water
horse#a solid-hoofed plant-eating domesticated mammal with a flowing mane and tail, used for riding
television#a system for transmitting visual images and sound that are reproduced on screens
soup#a liquid dish, typically made by boiling meat, fish, or vegetables, etc.
bottle#a container, typically made of glass or plastic and with a narrow neck
barber#a person who cuts hair
toast#sliced bread browned on both sides by exposure to radiant heat
radar#a system for detecting the presence, direction, distance, and speed of aircraft, ships, and other objects
red#of a color at the end of the spectrum next to orange and opposite violet
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
class Calc {
[Code] ....
when i built it, it showed the following errors:
1>------ Build started: Project: rough, Configuration: Debug Win32 ------
1> rough.cpp
1>e:c programs
ough
ough
ough.cpp(17): error C3872: '0xa0': this character is not allowed in an identifier
1>e:c programs
[Code] ....
Need sorting out the errors!!!
I have one base class and one derived class.
class Base
{
protected:
int a, b;
[Code]....
The two definition of the derived class constructor
(Derived(int aa, int bb, int cc) { Base(aa, bb); c = cc; }
and
Derived(int aa, int bb, int cc) : Base(aa, bb), c(cc) {}
looks identical but first one does not work.
So, I wounder what the difference between them?
I am trying to compile the files file1.c and file2.c using Mingw (gcc)
/////////////////////
header.h
////////////////////
#ifndef header
#define header
int variable;
#endif
[Code] ....
I would have expected a multiple defnition error when linking the two .c files. as in both the files, with the 'int variable' command, the variable 'variable' is defined (memory allocated) and during linking the linker doesnot know which variable to link to.
I get an error though when i use "int variable =123;" in the header file instead of the "int variable;" statement. i dont understand as in both the cases the variable is defined (memory is allocated) and the linker should give a multiple definition error.
I was trying to apply what is here (as someone who writes rarely and has to relearn everything each time): [URL] ....
I'm using a header file to define the structure:
#ifndef EINSTEIN_H
#define EINSTEIN_H
#include <stdio.h>
#include <vector>
struct SizeAll{
double year;
double R;
SizeAll() { year = 0; R = 0; }
[Code] ...
This creates quite a mess. It seems that somehow the "vector" declaration isn't working as the referenced web link seems to suggest that it should. I presume that, as usual, clearing one real error will eliminate the cascade of errors the first real error produces. Why won't VC++ accept the "vector" identifier?
The error messages that follow the build attempt are:
Friedman.cpp
d:documents and settingsxxmy documentsvisual studio 2010projectsfriedmanfriedmanEinstein.h(22):
warning C4996: 'fopen': This function or variable may be unsafe.
Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
[Code] .....
Can typedef and struct be put inside a class like below?
Code:
class classA {
private:
typedef void(classA::*aFnPtr_t) (); //pointer-to-member function
struct strctA {
int a;
int b[100];
};
typedef struct strctA strctA_t;
typedef void (classA::*bFnPtr_t)(strctA_t*); //pointer-to-member function
...
}
The compiler says my Rotor's cctor's definition doesn't match it's prototype.
Also, how can I convert my for loops into foreach loops?
Attached is the project.
"You cannot initialize the static data member in the class definition — that’s simply a blueprint for an object and initializing values for members are not allowed. You don’t want to initialize it in a constructor, because you want to increment it every time the constructor is called so the count of the number of objects created is accumulated."
Why don't you want to initialize it in a constructor?
Edit: Because every time it is called it will set it back to 0 or whatever the initializing value.