简体   繁体   中英

How to define project wide constants or how to make my own class' memberfunction accept const objects in c++

In my C++ project I've got a number of constants that I want to access from different classes . I put them all into a .h file that I named constants.h (I am not sure if this is the best idea, but it helped me keeping things sorted) Now I have the problem, that if I include that in more than one cpp file, I get LNK2005 errors:

------ Build started: Project: testMy3Dpoints, Configuration: Debug Win32 ------ Compiling...
testMy3Dpoints.cpp
Linking...
calculateDotproduct.obj : error LNK2005: "int myint" (?myint@@3HA) already defined in TestMy3Dpoints.obj
calculateDotproduct.obj : error LNK2005: "class v3d vect2" (?vect2@@3Vv3d@@A) already defined in testMy3Dpoints.obj
calculateDotproduct.obj : error LNK2005: "class v3d vect1" (?vect1@@3Vv3d@@A) already defined in testMy3Dpoints.obj
D:\\3D mapping\\visual studio projects\\testMy3Dpoints\\Debug\\testMy3Dpoints.exe : fatal error LNK1169: one or more multiply defined symbols found

When I was using only integers, defining all the variables as const made these errors disappear an the program compiled and run fine. But since I started introducing constants that are objects of my class v3d, this yields a different error: error C2662: 'v3d::dotProduct' : cannot convert 'this' pointer from 'const v3d' to 'v3d &' Conversion loses qualifiers Apparently it doesn't like cons v3d variables (constants). Is there anything wrong in the way I define the member function v3d::dotProduct?

This is my code:

testMy3Dpoints.cpp

#include "stdafx.h"
#include <iostream>
#include "v3d.h"
#include "constants.h"
#include "calculateDotproduct.h"

int _tmain(int argc, _TCHAR* argv[])
{


double dp = vect1.dotProduct(vect2);

std::cout << "myint: " << myint << std::endl;
std::cout << "vect1.x " << vect1.x << ", vect1.y " << vect1.y << ", vect1.z " << vect1.z << std::endl;
std::cout << "vect2.x " << vect2.x << ", vect2.y " << vect2.y << ", vect2.z " << vect2.z << std::endl;
std::cout << "dp = " << dp << std::endl;

alsoCalculateDotProduct();

return 0;

}

calculateDotproduct.h

#pragma once
void alsoCalculateDotProduct(

calculateDotproduct.cpp
#include "stdafx.h"
#include <iostream>
#include "v3d.h"
#include "constants.h"
#include "calculateDotproduct.h"

void alsoCalculateDotProduct()
{
double mydp = vect1.dotProduct(vect2);

std::cout << "vect1.x " << vect1.x << ", vect1.y " << vect1.y << ", vect1.z " << vect1.z << std::endl;
std::cout << "vect2.x " << vect2.x << ", vect2.y " << vect2.y << ", vect2.z " << vect2.z << std::endl;
std::cout << "mydp = " << mydp << std::endl;

return;
}

v3d.h:

#pragma once

class v3d
{
public:
double x;
double y;
double z;
v3d(double a, double b, double c);
void set(double a, double b, double c);
double dotProduct(v3d vector);
};

v3d.cpp:

#include "stdafx.h"
#include "v3d.h"    
void v3d::set(double a, double b, double c)
{
x=a;
y=b;
z=c;
}
v3d::v3d(double a, double b, double c)
{
set(a,b,c);
}
double v3d::dotProduct(v3d vector)
{
return x*vector.x + y*vector.y + z*vector.z;
}

constants.h

#pragma once 
#include "v3d.h"
const v3d vect1(1, 2, 3.14);
const  v3d vect2(4, 1.5, 0);
const int myint = 3;

respectively

#pragma once 
#include "v3d.h"
v3d vect1(1, 2, 3.14);
v3d vect2(4, 1.5, 0);
int myint = 3;

Object with external linkage must be defined once. In

v3d vect1(1, 2, 3.14);

vect1 has external linkage. So you'd have to use

extern V3d vect1;

in the .h and then

v3d vect1(1, 2, 3.14);

in one .cpp. There is a special rules which makes

const int myint = 3;

have internal linkage if it is never used as a lvalue, which could happen unexpectedly it is fed as a reference parameter or in something like:

x = v ? myint : myint2;

(Note that in general global variables are frowned upon).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM