Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 14 years ago.
Improve this question
Simply put:
foo.h:
#include "bar.h"
class foo {
private:
bar it;
void DoIt();
}
bar.h:
class bar {
public:
void Test();
}
foo.cpp:
void foo::DoIt() {
it.Test();
}
This will result in a:
error LNK2001: unresolved external symbol
Why?
You have not written the code for bar::Test() method.
I'm dumb! I did define Test() in the class definition but did not actually have an existing Test() function :(
Sorry.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 12 months ago.
Improve this question
I currently have a namespace set up like this:
SomeClass.h
namespace somenamespace {
class SomeClass {
public:
foo();
}
}
SomeClass.cpp
namespace somenamespace {
SomeClass::foo() {
somehelperfunction();
}
}
void somehelperfunction() {
std::cout << "hejflsdjf\n";
}
Without changing my header file, I cannot find a way to implement this helper function in a way which allows my class implementation to access the helper function. I was under the impression that as long as the helper function was in the same file I would be able to access it within the class implementation. But I get a "undeclared identifier" error when trying to build.
Functions must be declared before called.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So I have test.h which contains:
#ifndef TEST_H_
#define TEST_H_
class test {
public:
int value;
};
#endif /* TEST_H_ */
and my main.cpp:
#include "test.h"
class Magic {
test x;
x.value = 2; // Syntax error
};
int main () {
test y;
y.value = 2; // Works fine
return 0;
}
Why is this happening?
Assigning values like that is not valid syntax in a class definition in c++. The error has nothing to do with headers or whatever. Try putting everything in a single file and you will see the same behavior.
If you want to have default initialization of x.value to 2 for each instance of Magic define this in the constructor of Magic:
class Magic {
test x;
Magic() {
x.value = 2;
}
};
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have encountered one very strange problem. I am trying to explain full scenario here. Please suggest some solution.
/* "test.h" */
class A : public B
{
public:
A();
bool isUp;
};
/* test.cpp */
#include "test.h"
A::A()
{
isUp = false; //`isUp' was not declared in this scope
}
What is it means if I am declaring it in .h inside class. If I am wrong then what approach I need to follow.
EDIT :
class B
{
public:
sem_t m_job_count; //added by RajaGopal
B();
void Init();
void Init(char * s,int );
void RegisterWorker(worker *aWorker);
unsigned long getIndex();
void setIndex(unsigned long index);
char Msg[200];
static void* ThreadProc(void *p);
~B();
};
Where is the definition of class B. Since you are inheriting class B, compiler needs to know the definition of class B. Include its header file or its definition too.
Class definition of B should be visible to A.
Otherwise, I have compiled this code here and did not faced any problem.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
can we call static function from constructor of same class.
class a{
static void fun();
a() {fun();}
};
it is giving an error while linking code. I am using visual studio C++, 2010.
Yes you can - as long as you provide a function definition for the static function as well.
I don't really understand the question.
If you provide a function definition as said by Billz and Ogni42, it will work.
The following code does compile, and work:
#include <iostream>
class a {
public:
a() { fun(); }
private:
static void fun();
};
void a::fun() {
std::cout << "BOAP" << std::endl;
}
int main() {
a foo;
return 0;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am learing c++
All I want to know is I made a three file in code blocks using GCC compiler main.cpp printing.h printing.cpp
And I want to print the text in function "printer" in class "printingarea" in the file "printing.cpp" and want to prototype it in "printing.h" and want to call in "main.cpp"
Based on Claudios answer:
printing.h
class PrintingArea{
public:
void printer();
};
printing.cpp
#include "printing.h"
#include <iostream>
void PrintingArea::printer() {
std::cout << "Hello, World !";
}
main.cpp
#include "printing.h"
int main()
{
PrintingArea pa;
pa.printer();
return 0;
}
As you see, you define the prototype of the class in the header, and the implementation of the methods in the cpp.