I have two classes SHCalculator and SphericalLightProbe in separate files. I completely stripped them down to present the error I'm getting so these are the four files:
SHCalculator.h
#pragma once
namespace SphericalHarmonics
{
class SHCalculator
{
private:
public:
void Test();
SphericalLightProbe Test2();
};
}
SHCalculator.cpp
#include "pch.h"
#include "SphericalLightProbe.h"
#include "SHCalculator.h"
namespace SphericalHarmonics
{
void SHCalculator::Test()
{
SphericalLightProbe sp(2, 3.4f);
for (int i = 0; i < sp.coeffs.size(); i++)
{
std::cout << sp.coeffs[i] << std::endl;
}
}
SphericalLightProbe SHCalculator::Test2()
{
SphericalLightProbe sp(2, 4.3f);
for (int i = 0; i < sp.coeffs.size(); i++)
{
std::cout << sp.coeffs[i] << std::endl;
}
return sp;
}
}
SphericalLightProbe.h
#pragma once
namespace SphericalHarmonics
{
class SphericalLightProbe
{
private:
public:
SphericalLightProbe() { }
SphericalLightProbe(int numCoeffs);
SphericalLightProbe(int numCoeffs, float value);
std::vector<Eigen::Vector3f> coeffs;
};
}
SphericalLightProbe.cpp
#include "pch.h"
#include "SphericalLightProbe.h"
namespace SphericalHarmonics
{
SphericalLightProbe::SphericalLightProbe(int numCoeffs)
{
coeffs.resize(numCoeffs);
}
SphericalLightProbe::SphericalLightProbe(int numCoeffs, float value)
{
coeffs.resize(numCoeffs, Eigen::Vector3f(value, value, value));
}
}
This gives me a bunch of errors:
C2238 unexpected token(s) preceding ';'
C2059 syntax error: '('
Message see declaration of 'SphericalHarmonics::SHCalculator'
E0020 identifier "SphericalLightProbe" is undefined
C3646 'Test2': unknown override specifier
C2039 'Test2': is not a member of 'SphericalHarmonics::SHCalculator'
If I remove Test2() by commenting it out, all errors disappear. I'm trying to understand why.
You are missing the includes in your SHCalculator.h header;
Try something like:
#pragma once
#include "SphericalLightProbe.h"
namespace SphericalHarmonics
{
class SHCalculator
{
private:
public:
void Test();
SphericalLightProbe Test2();
};
}
Note that if your SHCalculator.cpp file includes above, the duplicate #include "SphericalLightProbe.h" line is not required.
But if you want to avoid for whatever reason the include, use forward declaration instead, like:
#pragma once
namespace SphericalHarmonics {
class SphericalLightProbe;
// ...
} // namespace SphericalHarmonics
Note that forward declaration normally does not force you to switch to pointers (like SphericalLightProbe *).
Related
When trying to compile my program I get these errors
1>c:\users\danilo\desktop\lab2\project1\project1\main.cpp(11): error C2275: 'Fighter': illegal use of this type as an expression
1>c:\users\danilo\desktop\lab2\project1\project1\fighter.h(9): note: see declaration of 'Fighter'
1>c:\users\danilo\desktop\lab2\project1\project1\main.cpp(11): error C2146: syntax error: missing ')' before identifier 'f1'
1>c:\users\danilo\desktop\lab2\project1\project1\main.cpp(12): error C2059: syntax error: '}'
1>c:\users\danilo\desktop\lab2\project1\project1\main.cpp(12): error C2143: syntax error: missing ';' before '}'
My main file looks like this:
#include "Fighter.h"
#include "Spell.h"
#include "Player.h"
#include "Wizard.h"
#include "Collection.h"
int lastID = 0;
int main{
Fighter f1;
f1("A", 100, 100);
};
and my Fighter.h looks like this
#define FIGHTER_H
#include "Card.h"
#include <string>
#include <iostream>
using namespace std;
class Fighter : public Card {
int power;
public:
virtual string getCategory() const override {
return "FIGHTER";
}
int getPower() const {
return power;
}
Fighter(string Name_, int neededEnergy_, int power_) : Card(Name_, neededEnergy_), power(power_) {}
void operator>(const Fighter& f) const {
if (this->getPower() > f.getPower()) {
cout << this->getName() << " is stronger " << endl;
}
else {
cout << f.getName() << " is stronger " << endl;
};
}
virtual void write(ostream& it) const override {
it << "(power: " << getPower() << ")";
}
};
#endif FIGHTER_H
What is the problem here?
Your main function is missing its parentheses. This
int main{
should be
int main(){
Also, f1("A", 100, 100) isn't a constructor call, but a call to operator(), which you don't have. Do this instead:
Fighter f1("A", 100, 100);
Also, ensure that your guards are consistent. There's a #ifndef FIGHTER_H missing.
I want to call a class member function that's nested within a namespace from a different file, but I don't know how.
For example:
How to call a class member function someFunc() that's located in code.h and nested within namespace "program" from main.cpp.
//code.h
#include <iostream>
namespace program
class test {
private:
int x;
public:
test()
{
test::x = 10;
};
someFunc()
{
cout << x << " ";
};
};
There are some problems in your code
#include <iostream>
namespace program { // <-- braces missing
class test
{
private:
int x;
public:
test()
{
test::x = 10; // <-- test:: is unnecessary but no error
};
void someFunc() // <-- return type missing
{
std::cout << x << " "; // <-- namespace std missing
};
};
} // <-- braces missing
int main() {
program::test t;
t.someFunc();
}
I'm trying to make the following compile under VS 2008 SP1 C++ project, but the friend class statement doesn't seem to have any effect. (See error message in the last code snippet.)
What am I doing wrong with the friend definition?
// EncryptionTypes.h file
#pragma once
//#include "Encryption.h" //adding this line doesn't help
using namespace crypto;
struct FILE_DATA_CACHE{
FILE_DATA_CACHE()
{
};
~FILE_DATA_CACHE()
{
}
friend class CEncryption;
private:
bool _isIndexFileUsed()
{
return bResult;
}
};
then:
// Encryption.h
#pragma once
#include "EncryptionTypes.h"
namespace crypto
{
class CEncryption
{
public:
CEncryption(void);
~CEncryption(void);
private:
BOOL _openFile();
private:
FILE_DATA_CACHE gFData;
};
};
and lastly:
// Encryption.cpp
#include "StdAfx.h"
#include "Encryption.h"
namespace crypto
{
CEncryption::CEncryption(void)
{
}
CEncryption::~CEncryption(void)
{
}
void CEncryption::_openFile()
{
//The line below generates this error:
//1>.\Encryption.cpp(176) : error C2248: 'FILE_DATA_CACHE::_isIndexFileUsed' : cannot access private member declared in class 'FILE_DATA_CACHE'
//1> c:\users\blah-blah\EncryptionTypes.h(621) : see declaration of 'FILE_DATA_CACHE::_isIndexFileUsed'
//1> c:\users\blah-blah\EncryptionTypes.h(544) : see declaration of 'FILE_DATA_CACHE'
gFData._isIndexFileUsed();
}
};
You have a circular dependency problem.
Encryption.h needs FILE_DATA_CACHE, which is defined in EncryptionTypes.h.
EncryptionType.h needs CEncryption, which is defined in Encryption.h.
Fortunately, you can get by with using a forward declaration of CEncryption in EncryptionType.h.
Modify EncryptionType.h to:
// EncryptionTypes.h file
#pragma once
// Can't #include Encryption.h. That will lead to circular
// #includes.
namespace crypto
{
// Forward declaration of crypto::CEncryption
class CEncryption;
}
struct FILE_DATA_CACHE{
FILE_DATA_CACHE()
{
};
~FILE_DATA_CACHE()
{
}
friend class crypto::CEncryption;
private:
bool _isIndexFileUsed()
{
return bResult;
}
};
I'm supposed to create a template class for an assignment, but I'm getting lots of different errors that I don't really understand, can someone please help me? I have attached the cp and header files that I wrote. I know this is probably very simple but I'm new to this, thank you!
#ifndef __Template_example__Initialisedchecker__
#define __Template_example__Initialisedchecker__
#include <stdio.h>
template <class data>
class Initialisedchecker
{
private:
data item;
bool definedOrN;
public:
Initialisedchecker()
{
definedOrN = false;
}
void setItem(const data&)
{
std::cin >> item;
definedOrN = true;
}
void displayItem()
{
if (definedOrN)
{
std::cout << item;
}
else
{
std::cout << "error, your item is undefined";
}
}
};
#endif
And this is the main:
#include <iostream>
#include "Initialisedchecker.h"
using namespace std;
int main()
{
item <int> x;
displayItem();
x = 5;
displayItem();
}
Sorry, I forgot to add the errors I'm getting, the header file doesn't give any errors, but in the main, it says:
Use of undeclared identifier 'display item' ,
Use of undeclared identifier 'item' ,
Use of undeclared identifier 'x' ,
Expected a '(' for function-style cast or type construction
The class template is called Initialisedchecker, not item. And you need to call the member function on the object. You need:
int main()
{
Initialisedchecker <int> x;
x.displayItem();
// this is strange: x = 5;
// maybe use:
// x.setItem( 5 );
x.displayItem();
}
I want my getFeatures function to return a vector of structs. The struct I have declared in the header file, but when in my .cpp file I try to write my function name I get a use of undeclared identifier error. The identifier is declared and I can use it in other functions. Also when I write my return statement I get a no viable conversion of vector type to int error. In my header file I have the function prototype and it states my function should return a vector of my struct type, not of type int. What on earth is going on?
#include "Feature_Builder.h"
Feature_Builder::Feature_Builder()
{
int input;
vector<int> intImageVals, temp;
fstream file;
file.open("Integral_Images.txt", ios::in);
if (file.fail())
{
cerr << "File not open!\n";
exit(EXIT_FAILURE);
}
for (int i=0; i<12876; i++)
{
file >> input;
while (input != -1)
{
intImageVals.push_back(input);
file >> input;
}
buildFeatureOne(intImageVals);
buildFeatureTwo(intImageVals);
buildFeatureThree(intImageVals);
buildFeatureFour(intImageVals);
featureIndex = 0;
intImageVals.clear();
}
}
/*****************************************************************************
This is the function with errors! XCode says featureValues is an undeclared
identifier and that features should be of type int!
*****************************************************************************/
vector<featureValues> Feature_Builder::getFeatures()
{
return features;
}
And my header file
#ifndef Facial_Learner_Feature_Builder_h
#define Facial_Learner_Feature_Builder_h
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class Feature_Builder
{
private:
void buildFeatureOne(vector<int>);
void buildFeatureTwo(vector<int>);
void buildFeatureThree(vector<int>);
void buildFeatureFour(vector<int>);
struct featureValues
{
private:
vector<int> vals;
public:
inline void pushValue(int value)
{vals.push_back(value);}
inline int getValue(int i)
{return vals[i];}
};
int featureIndex;
vector<featureValues> features;
public:
Feature_Builder();
vector<featureValues> getFeatures();
};
#endif
You need the correct scoping for the inner structure:
vector<Feature_Builder::featureValues> Feature_Builder::getFeatures() { ... }